Simple python Solution w/ explanation


  • 0
    M

    The trick to use is that temperature is integer and limited (only 71 numbers).
    We create an array of size 71 called arr. Then we walk back from end of temperatures and for each temperature we keep the most recent index in arr.
    For each temperature t we look at tempratures bigger than t in arr and find the minimum index.

    class Solution(object):
        def dailyTemperatures(self, temperatures):
            """
            :type temperatures: List[int]
            :rtype: List[int]
            """
            arr = [-1 for _ in range(71)]
            res = [0] * len(temperatures)
    
            for i in range(len(temperatures)-1,-1,-1):
                t = temperatures[i]
                j = [x for x in arr[t-30+1:] if x > -1]
                res[i] = 0 if len(j) == 0 else min(j)-i
                arr[t-30] = i
    
            return res
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.