Skip to content

Commit

Permalink
add solution for 739. Daily Temperatures in Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chkid authored and t3chkid committed Aug 2, 2022
1 parent 1f7b2f3 commit c9ddb34
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions kotlin/739-Daily-Temperatures.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.*

class Solution {
fun dailyTemperatures(temperatures: IntArray): IntArray {
if (temperatures.size == 1) return intArrayOf(0)
val stack = Stack<Int>()
val resultantArray = IntArray(temperatures.size)
var poppedElement: Int
for (i in 0..temperatures.lastIndex) {
while (stack.isNotEmpty() && temperatures[stack.peek()] < temperatures[i]) {
poppedElement = stack.pop()
resultantArray[poppedElement] = i - poppedElement
}
stack.push(i)
}
return resultantArray
}
}

0 comments on commit c9ddb34

Please sign in to comment.