[Leetcode] 739. Daily Temperatures
                    
                2021. 1. 18. 11:25ㆍProblem Solving/LeetCode
Leetcode 739. 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
Solution
class Solution:
    def dailyTemperatures(self, T: List[int]) -> List[int]:
        stack = []
        result = [0]*len(T)
        for i, n in enumerate(T):
            while stack and n > T[stack[-1]]:
                top = stack.pop()
                result[top] = i - top
            stack.append(i)
        return result

- result 리스트를 T의 길이만큼 0으로 초기화
 - 스택이 비어있으면 append해주고, 비어있지 않으면서 고점이 오면 
stack.pop()하여 인덱스의 차를 result에 저장해줌 
Reference
파이썬 알고리즘 인터뷰
![]()  | 
  | 
반응형
    
    
    
  'Problem Solving > LeetCode' 카테고리의 다른 글
| [Leetcode] 232. Implement Queue using Stacks (0) | 2021.01.18 | 
|---|---|
| [Leetcode] 225. Implement Stack using Queues (0) | 2021.01.18 | 
| [LeetCode] 20. Valid Parentheses (0) | 2021.01.18 | 
| [LeetCode] 328. Odd Even Linked List (0) | 2021.01.15 | 
| [LeeCode] 24. Swap Nodes in Pairs (0) | 2021.01.14 | 
