-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLeetCode739daily-temperatures.py
More file actions
executable file
·42 lines (38 loc) · 1.14 KB
/
LeetCode739daily-temperatures.py
File metadata and controls
executable file
·42 lines (38 loc) · 1.14 KB
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
42
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/10/5 10:01 AM
# @Author : Slade
# @File : LeetCode739daily-temperatures.py
class Solution(object):
def dailyTemperatures(self, T):
"""
:type T: List[int]
:rtype: List[int]
"""
dp = [0] * len(T)
for i in range(len(T) - 2, -1, -1):
for j in range(i + 1, len(T)):
if T[i] < T[j]:
dp[i] = j - i
break
# 如果历史上的已经比当前的都小就不用再去比了,c++可以pass,python OOT.
elif dp[j] == 0:
break
return dp
class Solution1(object):
def dailyTemperatures(self, T):
"""
:type T: List[int]
:rtype: List[int]
"""
stack = []
dp = [0] * len(T)
for k, v in enumerate(T):
while stack and stack[-1][1] < v:
ki, _ = stack.pop()
dp[ki] = k - ki
stack.append((k, v))
return dp
if __name__ == '__main__':
s = Solution1()
print(s.dailyTemperatures([73, 74, 75, 71, 69, 72, 76, 73]))