forked from javadev/LeetCode-in-Kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.kt
27 lines (25 loc) · 868 Bytes
/
Solution.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package g0701_0800.s0739_daily_temperatures
// #Medium #Top_100_Liked_Questions #Array #Stack #Monotonic_Stack #LeetCode_75_Monotonic_Stack
// #Programming_Skills_II_Day_6 #Big_O_Time_O(n)_Space_O(n)
// #2022_09_13_Time_936_ms_(80.54%)_Space_79.5_MB_(63.09%)
class Solution {
fun dailyTemperatures(temperatures: IntArray): IntArray {
val sol = IntArray(temperatures.size)
sol[temperatures.size - 1] = 0
for (i in sol.size - 2 downTo 0) {
var j = i + 1
while (j <= sol.size) {
if (temperatures[i] < temperatures[j]) {
sol[i] = j - i
break
} else {
if (sol[j] == 0) {
break
}
j = j + sol[j]
}
}
}
return sol
}
}