SW Test/Programmers

[Programmers] Lv2. ๊ธฐ๋Šฅ๊ฐœ๋ฐœ (kotlin)

An effort will never betray ๐Ÿ˜Ž 2022. 8. 7. 17:42
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/42586?language=kotlin

 

 

 

์˜ˆ์‹œ

https://school.programmers.co.kr/learn/courses/30/lessons/42586?language=kotlin

 

 

ํ’€์ด

  • ์Šคํƒ/ํ๋ฅผ ์ด์šฉํ•œ ๋Œ€ํ‘œ์ ์ธ ๋ฌธ์ œ๋กœ ๊ฐ ๊ธฐ๋Šฅ๋ณ„ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ days ๋ฐฐ์—ด์— ๋‹ด์Šต๋‹ˆ๋‹ค.
  • ์Šคํƒ๋ฅผ ์ด์šฉํ•ด๋„ ๋˜์ง€๋งŒ elem์ด๋ผ๋Š” ๋ณ€์ˆ˜๊ฐ€ top์„ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ํ•˜์—ฌ top๋ณด๋‹ค ์ž‘์€ ๊ธฐ๊ฐ„์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ  answer์— ๋”ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
import kotlin.math.*

class Solution {
    fun solution(progresses: IntArray, speeds: IntArray): IntArray {
        var answer = mutableListOf<Int>()
        val days = progresses.mapIndexed{ index, elem ->
            ceil(((100-elem).toFloat() / speeds[index]).toDouble()).toInt()
        }
        
        var elem = days.first()
        var cnt = 1
        for(i in 1 until days.size){
            if(elem < days[i]){
                answer.add(cnt)
                elem = days[i]
                cnt = 1
            }else{
                cnt++
            }
        }
        answer.add(cnt)
        
        return answer.toIntArray()
    }
}

 

 

 

์ฐธ๊ณ 

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