Daily Temperatures (Leetcode 739)

Problem Link: https://leetcode.com/problems/daily-temperatures/

class Solution:
    def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
        
        n=len(temperatures)
        s=[]
        ans=[]
        for i in range(n-1,-1,-1):
            if(len(s)==0):
                ans.append(0)
            else:
                if(temperatures[s[-1]]>temperatures[i]):
                    ans.append(s[-1]-i)
                else:
                    while(len(s)>0 and temperatures[s[-1]]<=temperatures[i]):
                        s.pop()
                    if(len(s)==0):
                        ans.append(0)
                    else:
                        ans.append(s[-1]-i)
            s.append(i)
        return ans[::-1]     

Last updated