python-algorithm

leetcode 739. Daily Temperatures

무적김두칠 2023. 2. 23. 23:07

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

 

Daily Temperatures - LeetCode

Can you solve this real interview question? Daily Temperatures - Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        answer = [0* len(temperatures)
        stack = []
        for i, cur in enumerate(temperatures):
            while stack and cur > temperatures[stack[-1]]:
                last = stack.pop()
                answer [last] = i -last
            stack.append(i)
 
        return answer
cs
반응형