๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/Kotlin

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค • ์ฝ”ํ‹€๋ฆฐ] ๋ง์น ํ•˜๊ธฐ #161989

by ํ‚ค์œค 2024. 1. 5.

#161989

๐ŸŽ„ Question ?

class Solution {
    fun solution(n: Int, m: Int, section: IntArray): Int {
        var answer: Int = 0
        return answer
    }
}

๐Ÿงฉ Thought Process

  1. ์•„์˜ˆ ๋ชปํ’€์—ˆ๋‹ค..

 

๐ŸŽ€ Answer

class Solution {
    fun solution(n: Int, m: Int, section: IntArray): Int {
        //ํ•œ ๊ตฌ์—ญ์— ํŽ˜์ธํŠธ๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์น ํ•ด๋„ ๋˜๊ณ  ๋‹ค์‹œ ์น ํ•ด์•ผ ํ•  ๊ตฌ์—ญ์ด ์•„๋‹Œ ๊ณณ์— ํŽ˜์ธํŠธ๋ฅผ ์น ํ•ด๋„ ๋˜์ง€๋งŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ๋กœ ์ •ํ•œ ๊ตฌ์—ญ์€ ์ ์–ด๋„ ํ•œ ๋ฒˆ ํŽ˜์ธํŠธ์น ์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
        val queue = LinkedList<Int>()
        var answer = 0
        section.toCollection(queue)
        var currentPoint = Int.MIN_VALUE

        while (queue.isNotEmpty()) {
            val node = queue.poll()
            if (currentPoint + m - 1 < node) {
                answer++
                currentPoint = node
            }
        }

        return answer
    }
}

java์˜ linkedlist๋ฅผ importํ•ด์„œ ํ’€์ดํ•ด ์ฃผ์—ˆ๋‹ค.

๐ŸŽ Result

 

๐Ÿ† Comment