aboutsummaryrefslogtreecommitdiff
path: root/daily_temps.py
blob: f96e9ecffea03b6a781915d98affbf532d66ef63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 739. Daily Temperatures

"""
given an array of integers 'temperatures' represents the daily temperatures
return an array 'answer' such that 'answer[i]' is the number of days you have
to wait after the i'th day to get a warmer temperature. if there is no future
day for which this is possible, keep 'answer[i] == 0' instead.
"""


class Solution(object):
    def dailyTemperatures(self, temperatures):
        """
        :type temperatures: List[int]
        :rtype: List[int]
        """
        ans = [0] * len(temperatures)
        stack = []
        for i, t in enumerate(temperatures):
            while stack and temperatures[stack[-1]] < t:
                curr = stack.pop()
                ans[curr] = i - curr
            stack.append(i)
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(obj.dailyTemperatures(temperatures=[73, 74, 75, 71, 69, 72, 76, 73]))
    print(obj.dailyTemperatures(temperatures=[30, 40, 50, 60]))
    print(obj.dailyTemperatures(temperatures=[30, 60, 90]))