알고리즘 문제 풀이

프로그래머스 - 기능개발

블린더르 2020. 8. 5. 23:09
import kotlin.math.ceil

/*
 * 2020-08-05
 * https://programmers.co.kr/learn/courses/30/lessons/42586?language=kotlin
 */
class FunctionDevelopment {
    fun solution(progresses: IntArray, speeds: IntArray): IntArray {
        var answer = ArrayList<Int>()
        val workingDays = progresses.mapIndexed { i, progress ->
            ceil((100-progress).div(speeds[i].toDouble())).toInt()
        }

        var prevWork = workingDays.first()
        var cnt = 1

        workingDays.slice(1 until workingDays.count()).forEach {
            when {
                prevWork < it ->{
                    answer.add(cnt)
                    prevWork = it
                    cnt = 1
                }
                prevWork >= it -> {
                    cnt++
                }
            }
        }
        answer.add(cnt)
        return answer.toIntArray()
    }
}

fun main() {
    val result = FunctionDevelopment().solution(
        intArrayOf(93, 30, 55), intArrayOf(1, 30, 5)
    )
    println(result)
}
반응형