Problem Solving/LeetCode
[Leetcode] 739. Daily Temperatures
yuseon-Lim
2021. 1. 18. 11:25
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
파이썬 알고리즘 인터뷰
![]() |
|
반응형