원본 문제 : https://programmers.co.kr/learn/courses/30/lessons/42586
<첫번째>
import java.util.*
class Solution {
fun solution(progresses: IntArray, speeds: IntArray): IntArray {
val finalDays: LinkedList<Int> = LinkedList()
for( i in progresses.indices) {
if ( (100 - progresses[i])%speeds[i] == 0 )
finalDays.add( (100 - progresses[i])/speeds[i] )
else
finalDays.add( (100 - progresses[i])/speeds[i] + 1)
}
val result: MutableList<Int> = mutableListOf()
var first = finalDays.poll()
var count = 1
while (!finalDays.isEmpty()) {
var second = finalDays.poll()
if (first >= second) count ++
else {
result.add(count)
count = 1
first = second
}
}
result.add(count)
var answer = IntArray(result.size)
for ( i in 0 until result.size ) {
answer[i] = result[i]
// println("answer[$i]: ${answer[i]}")
}
return answer
}
}
<두번째> 2020/06/07
import java.util.*
fun solution(progresses: IntArray, speeds: IntArray): IntArray {
val finalDays: LinkedList<Int> = LinkedList()
val result = mutableListOf<Int>()
progresses.forEachIndexed { index, value ->
if ( (100 - value) % speeds[index] == 0 ) {
finalDays.add( (100 - value) / speeds[index] )
}
else {
finalDays.add( (100 - value) / speeds[index] + 1)
}
}
while (!finalDays.isEmpty()) {
if (finalDays.size == 1) {
result.add(1)
break
}
var count: Int = 0
val first = finalDays.peek()
run {
finalDays.forEach {
if (it <= first)
count++
else {
return@run
}
}
}
result.add(count)
while (count-- > 0) {
finalDays.poll()
}
}
return result.toIntArray()
}
'프로그래머스 > 코딩연습1' 카테고리의 다른 글
[프로그래머스] 탑(42588)(Kotlin) (0) | 2020.01.16 |
---|---|
[프로그래머스] 체육복(42862)(Kotlin) (0) | 2020.01.16 |
[프로그래머스] 실패율 (42889)(Kotlin) (0) | 2020.01.14 |
[프로그래머스] 프린터 (42587)(Kotlin) (0) | 2020.01.13 |
[프로그래머스] 다리를 지나는 트럭(42583)(Kotlin) (0) | 2020.01.13 |
댓글