739. Daily Temperatures

https://leetcode.com/problems/daily-temperatures/

标准的decreasing stack模板 循环中 while loop 之后再加入当前值

class Solution:
    def dailyTemperatures(self, T: List[int]) -> List[int]:
        n = len(T)
        res = [0] * n
        stack = []
        for i in range(n):
            while stack and T[stack[-1]] < T[i]:
                curr = stack.pop()
                res[curr] = i - curr
            stack.append(i)
        return res
        

Last updated

Was this helpful?