[Leetcode]739. Daily Temperatures(x)

2024. 8. 2. 08:08Algorithm

1. problem : 

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

 

2. solution 1 :

class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        ans = [0] * len(temperatures) 
        stack = [] 
        for i,temp in enumerate(temperatures):
            while stack and temperatures[stack[-1]] < temp:
                idx = stack.pop()
                ans[idx] = i - idx 
            stack.append(i) 
        return ans