SW Test/Programmers

[Programmers] Lv1. ๋ง์น ํ•˜๊ธฐ(Kotlin)

An effort will never betray ๐Ÿ˜Ž 2023. 5. 3. 00:09
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

 

์˜ˆ์‹œ

 

 

ํ’€์ด

  • n ๊ฐœ์˜ ํฌ๊ธฐ ๋ฐฐ์—ด์„ ๋งŒ๋“  ํ›„ section์—์„œ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ธ๋ฑ์Šค์—๋Š” true๋กœ ํ‘œ๊ธฐํ•ฉ๋‹ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  section์˜ ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ํ•ด๋‹น ์ธ๋ฑ์Šค๊ฐ€ true๋ฉด ํŽ˜์ธํŠธ ์น ์„ ๋ฌด์กฐ๊ฑด ํ•ด์•ผํ•˜๋‹ˆ answer ์— 1์„ ๋”ํ•ฉ๋‹ˆ๋‹ค.
  • ์ดํ›„ ํŽ˜์ธํŠธ ์น ์˜ ํฌ๊ธฐ์ธ m ๋งŒํผ ์ธ๋ฑ์Šค๋ฅผ ๋”ํ•œ ํ›„ ํ•˜๋‚˜์”ฉ ๋ฐฐ์—ด์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
  • ํ•ด๋‹น ์ธ๋ฑ์Šค๊ฐ€ false๋ฉด 1์”ฉ ๋”ํ•ด ํŽ˜์ธํŠธ ์น ํ•ด์•ผ ํ•˜๋Š” ๊ณณ์„ ์ฐพ๊ณ  n ๊นŒ์ง€ ํ™•์ธํ•˜์˜€์œผ๋ฉด ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.
class Solution {
    fun solution(n: Int, m: Int, section: IntArray): Int {
        var answer: Int = 0
        val arr = Array(n+1){false}
        section.forEach{ arr[it] = true}
        var idx = section.first()
        
        while(idx <= n){
            if(arr[idx] == true){
                answer++
                idx += m
            }else{
                idx++
            }
        }
        
        return answer
    }
}

 

 

 

์ฐธ๊ณ 

  • ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ธ kotlin์˜ ํŠน์„ฑ์— ๋งž๊ฒŒ ํ•จ์ˆ˜๋ฅผ ๋งŽ์ด ์ตํžˆ์ž ๐Ÿ˜ฝ
๋ฐ˜์‘ํ˜•