파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 12. 18. 23:45

https://leetcode.com/problems/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

단순히 풀면 O(n^2)으로 추정되는데,
그러면 10^10이라서 TL이 날 가능성이 매우 높음.

다른 사람이 푼 걸 보니, 뒤에서 부터 세어서 오면 매우 간단하게 풀림 =_=;;

class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        l = len(temperatures)
        ar = [-1] * 71
        res = [0] * l

        for i in range(l-1, -1, -1):
            t = temperatures[i]
            j = [x for x in ar[t-30+1:] if x > -1]
            res[i] = 0 if len(j) == 0 else min(j)-i
            ar[t-30] = i
        
        return res
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.12.20
Today's Challenge  (0) 2022.12.19
Today's Challenge  (0) 2022.12.17
Today's Challenge  (0) 2022.12.16
Today's Challenge  (0) 2022.12.15
Comments