Category | ps/자료구조 |
---|---|
Tag | 자료구조 |
Tags | stack |
난이도 | ★★ |
발행 여부 | |
사이트 | Leet code |
실수 유형 | |
이해 | 완벽히 이해 |
최종 편집 일시 | |
푼 날짜 |
문제 해설 및 주의사항
원문
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 i
th
day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0
instead.
Example 1:
Input: temperatures = [73,74,75,71,69,72,76,73] Output: [1,1,4,2,1,1,0,0]
Example 2:
Input: temperatures = [30,40,50,60] Output: [1,1,1,0]
Example 3:
Input: temperatures = [30,60,90] Output: [1,1,0]
번역 및 주의사항
answer
에 반환해라.
해당 index 에서 며칠이 지나야 더 따뜻한 날이 나오는지 answer
에 넣어라.
Constraints:
1 <= temperatures.length <= 10
5
30 <= temperatures[i] <= 100
풀이
내 풀이 코드
class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: answer = [ 0 for i in range(len(temperatures)) ] stack = [] stack.append(0) for idx, temperature in enumerate(temperatures) : if idx == 0: continue # stack 이 비지않고, 현재 온도가 stack 의 top 온도 보다 크다면 : 정답 처리 while stack and temperature > temperatures[stack[-1]]: stack_last = stack.pop() answer[stack_last] = idx - stack_last stack.append(idx) return answer
풀이 코드 (책)
py
퇴고
반응형