Leetcode 739 Solution

This article provides solution to leetcode question 739 (daily-temperatures)

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

Solution

class Solution:
    def dailyTemperatures(self, temp: List[int]) -> List[int]:
        s = []
        ans = []
        for i in range(len(temp) - 1, -1, -1):
            while s and temp[i] >= s[-1][0]:
                s.pop()

            if not s:
                ans.append(0)
            else:
                ans.append(s[-1][1] - i)

            s.append((temp[i], i))

        ans = reversed(ans)
        return ans