diff options
Diffstat (limited to 'daily_temps.py')
-rw-r--r-- | daily_temps.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/daily_temps.py b/daily_temps.py new file mode 100644 index 0000000..f96e9ec --- /dev/null +++ b/daily_temps.py @@ -0,0 +1,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])) |