LeetCode 739. Daily Temperatures

개발공부를해보자·2025년 1월 15일

LeetCode

목록 보기
22/95

파이썬 알고리즘 인터뷰 문제 22번(리트코드 739번) Daily Temperatures
https://leetcode.com/problems/daily-temperatures/

나의 풀이

class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        stack = []
        result = [0] * len(temperatures)

        for index, temperature in enumerate(temperatures):
            while stack and temperatures[stack[-1]] < temperatures[index]:
                day = stack.pop()
                result[day] = index - day
            stack.append(index)
        
        return result

오답, 배운 점

  • 처음에 스스로 풀지 못했는데, stack에 온도가 아닌 온도의 index를 저장할 생각을 떠올리지 못했다.
  • 풀이를 보고 보름 정도 지난 후 스스로 풀었다.
  • 생각해보면 묻는 것이 온도 차에 대한 이야기가 아니라 날짜의 차이기 때문에 index를 저장하는 것이 자연스럽고 당연하다. 지금 생각해보면 당연한데 그때는 왜 떠올리지 못했을까.
profile
개발 공부하는 30대 비전공자 직장인

0개의 댓글