forked from onlybooks/java-algorithm-interview
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP22_2.kt
More file actions
23 lines (21 loc) · 795 Bytes
/
P22_2.kt
File metadata and controls
23 lines (21 loc) · 795 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package ch09
import java.util.*
class P22_2 {
fun dailyTemperatures(temperatures: IntArray): IntArray {
// 결과를 담을 정수형 배열 선언
val result = IntArray(temperatures.size)
// 결과 처리를 위한 스택 선언
val stack: Deque<Int> = ArrayDeque()
for (i in temperatures.indices) {
// 현재 온도가 스택에 있는 온도보다 높다면 꺼내서 결과를 업데이트한다.
while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
val last = stack.pop()
// 결과 업데이트
result[last] = i - last
}
// 현재 인덱스를 스택에 삽입
stack.push(i)
}
return result
}
}