본문 바로가기
Programming/LeetCode

[LeetCode] Daily Temperatures

by 데이터현 2022. 3. 29.

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

 

Daily Temperatures - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

어떤 시간의 흐름에 따라 풀이하는 건 스택을 활용하면 쉽게 풀 수 있는 것 같다.

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

댓글