单调栈,一次遍历数组,当 stack 为空或者当前元素和栈顶元素不满足条件时入栈,满足条件时,依次出栈

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param temperatures int整型一维数组 
# @return int整型一维数组
#
class Solution:
    def temperatures(self , temperatures: List[int]) -> List[int]:
        # write code here
        stack, res = [], [0] * len(temperatures)
        for i, t in enumerate(temperatures):
            if not stack or temperatures[stack[-1]] >= t:
                stack.append(i)
            else:
                while stack and temperatures[stack[-1]] < t:
                    j = stack.pop()
                    res[j] = i - j
                stack.append(i)
        return res