Posted on 2024-01-31 18:05
Uriel 阅读(17)
评论(0) 编辑 收藏 引用 所属分类:
数据结构 、
闲来无事重切Leet Code
早前的写法是倒序->http://www.cppblog.com/Uriel/articles/229495.html
改了正序写法,快了不少
1 #739
2 #Runtime: 998 ms (Beats 89.58%)
3 #Memory: 23.6 MB (Beats 98.27%)
4
5 class Solution(object):
6 def dailyTemperatures(self, temperatures):
7 """
8 :type temperatures: List[int]
9 :rtype: List[int]
10 """
11 ans = [0] * len(temperatures)
12 stk = []
13 for i, t in enumerate(temperatures):
14 if not stk:
15 stk.append(i)
16 else:
17 while stk and temperatures[stk[-1]] < t:
18 idx = stk.pop()
19 ans[idx] = i - idx
20 stk.append(i)
21 return ans