File tree Expand file tree Collapse file tree 1 file changed +18
-15
lines changed Expand file tree Collapse file tree 1 file changed +18
-15
lines changed Original file line number Diff line number Diff line change 1
- class Solution (object ):
2
- def dailyTemperatures (self , T ):
3
- """
4
- :type T: List[int]
5
- :rtype: List[int]
6
- """
7
- res = [0 ] * len (T )
8
- s = []
9
- # print res
10
- for i in range (0 , len (T )):
11
- while (s and T [i ] > T [s [- 1 ]]):
12
- res [s [- 1 ]] = i - s [- 1 ]
13
- s .pop ()
14
- s .append (i )
15
- return res
1
+ # 单调栈解法
2
+ class Solution :
3
+ def dailyTemperatures (self , T : List [int ]) -> List [int ]:
4
+ # 可以维护一个存储下标的单调栈,从栈底到栈顶的下标对应的温度列表中的温度依次递减。
5
+ # 如果一个下标在单调栈里,则表示尚未找到下一次温度更高的下标。
6
+ ans = [0 ] * len (T )
7
+ stack = []
8
+ for i in range (len (T )):
9
+ while stack and T [stack [- 1 ]] < T [i ]: # 栈不为空 && 栈顶温度小于当前温度
10
+ ans [stack [- 1 ]] = i - stack [- 1 ]
11
+ stack .pop ()
12
+ stack .append (i )
13
+ return ans
14
+
15
+ # 作者:dz-lee
16
+ # 链接:https://leetcode.cn/problems/daily-temperatures/solutions/284113/dan-diao-zhan-cong-hou-xiang-qian-bian-li-python3x/
17
+ # 来源:力扣(LeetCode)
18
+ # 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
You can’t perform that action at this time.
0 commit comments