-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path739.py
41 lines (37 loc) · 1.22 KB
/
739.py
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
32
33
34
35
36
37
38
39
40
41
#==================================================
#==> Title: daily-temperatures
#==> Author: Zhang zhen
#==> Email: hustmatnoble.gmail.com
#==> GitHub: https://github.com/MatNoble
#==> Date: 1/15/2021
#==================================================
"""
https://leetcode-cn.com/problems/daily-temperatures/
"""
class Solution:
## 前
def dailyTemperatures(self, T):
res = [0] * len(T)
stack = []
for i in range(len(T)):
while stack and T[stack[-1]] < T[i]:
res[stack[-1]] = i - stack[-1]
stack.pop()
stack.append(i)
return res
## 后
def dailyTemperatures_1(self, T):
res = [0] * len(T)
stack = []
# for i in reversed(range(len(T))):
for i in range(len(T)-1, -1, -1):
while stack and T[i] >= T[stack[-1]]:
stack.pop()
if len(stack) != 0:
res[i] = stack[-1] - i
stack.append(i)
return res
temperatures = [73, 74, 75, 71, 69, 72, 76, 73]
mat = Solution()
print(mat.dailyTemperatures(temperatures))
print(mat.dailyTemperatures_1(temperatures))