본문으로 바로가기

[Leet code] 739. Daily Temperatures

category ps/자료구조 2021. 11. 26. 23:42
Categoryps/자료구조
Tag자료구조
Tagsstack
난이도★★
발행 여부
사이트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 ith 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 <= 105
  • 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


퇴고

반응형

'ps > 자료구조' 카테고리의 다른 글

[Leet code] 21. Merge Two Sorted Lists  (0) 2021.11.26
[Leet code] 20. Valid Parentheses  (0) 2021.11.26
[boj] 1874. 스택수열  (0) 2021.11.26
[boj] 10773. 제로  (0) 2021.11.26
771. Jewels and Stones  (0) 2021.11.26