Skip to content

Commit ec4ebd8

Browse files
authored
Merge pull request #718 from t3chkid/main
Adds solution for 739. Daily Temperatures in Kotlin
2 parents 1260aa5 + c9ddb34 commit ec4ebd8

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

kotlin/739-Daily-Temperatures.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import java.util.*
2+
3+
class Solution {
4+
fun dailyTemperatures(temperatures: IntArray): IntArray {
5+
if (temperatures.size == 1) return intArrayOf(0)
6+
val stack = Stack<Int>()
7+
val resultantArray = IntArray(temperatures.size)
8+
var poppedElement: Int
9+
for (i in 0..temperatures.lastIndex) {
10+
while (stack.isNotEmpty() && temperatures[stack.peek()] < temperatures[i]) {
11+
poppedElement = stack.pop()
12+
resultantArray[poppedElement] = i - poppedElement
13+
}
14+
stack.push(i)
15+
}
16+
return resultantArray
17+
}
18+
}

0 commit comments

Comments
 (0)