원본 문제 : https://programmers.co.kr/learn/courses/30/lessons/42588?language=kotlin
<첫번째>
import java.util.*
class Solution {
fun solution(heights: IntArray): IntArray {
val list: MutableList<Int> = mutableListOf()
val queue: LinkedList<Int> = LinkedList()
for (height in heights)
queue.add(height)
while (!queue.isEmpty()) {
var lastQueue = queue.pollLast()
for (i in (queue.size - 1) downTo 0) {
if (queue[i] > lastQueue) {
list.add(i + 1)
break
}
else if ( i == 0 )
list.add(0)
}
}
list.add(0)
var answer = IntArray(list.size)
for (i in list.indices)
answer[i] = list[i]
return answer.reversedArray()
}
}
<두번째> 2020/06/08
class Solution {
fun solution(heights: IntArray): IntArray {
val list = MutableList(heights.size) { 0 }
val heightsRevered = heights.reversedArray()
heightsRevered.forEachIndexed { rIndex, rValue ->
for (i in rIndex + 1 until heightsRevered.size) {
if (rValue < heightsRevered[i]) {
list[heightsRevered.size - 1 - rIndex] = heightsRevered.size - i
break
}
}
}
return list.toIntArray()
}
}
'프로그래머스 > 코딩연습1' 카테고리의 다른 글
[프로그래머스] 모의고사 (42840)(Kotlin) (0) | 2020.01.20 |
---|---|
[프로그래머스] 쇠막대기(42585)(Kotlin) (0) | 2020.01.16 |
[프로그래머스] 체육복(42862)(Kotlin) (0) | 2020.01.16 |
[프로그래머스] 기능개발(42586)(Kotlin) (0) | 2020.01.14 |
[프로그래머스] 실패율 (42889)(Kotlin) (0) | 2020.01.14 |
댓글