739. Daily Temperatures
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 67% Topics: Array, Stack, Monotonic Stack
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(n)
class Solution(object):
def dailyTemperatures(self, temperatures):
"""
:type temperatures: List[int]
:rtype: List[int]
"""
result = [0] * len(temperatures)
stk = []
for i in xrange(len(temperatures)):
while stk and \
temperatures[stk[-1]] < temperatures[i]:
idx = stk.pop()
result[idx] = i-idx
stk.append(i)
return result
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions