-
Notifications
You must be signed in to change notification settings - Fork 1
/
单调栈.py
53 lines (44 loc) · 1.29 KB
/
单调栈.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
42
43
44
45
46
47
48
49
50
51
52
53
# 单调栈问题
def next_greater_number(nums):
n = len(nums)
res = [0] * n
stack = []
for i in range(n-1, -1, -1):
# 如果栈顶元素比当前数字小直接弹出栈顶
while stack and stack[-1] <= nums[i]:
stack.pop()
res[i] = -1 if not stack else stack[-1]
# 加入新的栈顶
stack.append(nums[i])
return res
nums = [2, 1, 2, 4, 3]
res = next_greater_number(nums)
print(res)
# 496.下一个更大元素I
def next_greater_number2(nums1, nums2):
res = {}
n = len(nums1)
stack = []
# 因为nums1是nums2的子集,所以用nums2构建单调栈
for i in range(n-1, -1, -1):
while stack and stack[-1] <= nums2[i]:
stack.pop()
res[nums2[i]] = -1 if not stack else stack[-1]
stack.append(nums2[i])
# 获取数字对应的后一个大的数字
return [res[num] for num in nums1]
# 1118 一月有多少天
# 存索引 更方便一些
def daily_temperatures(nums):
n = len(nums)
res = [0] * n
stack = []
for i in range(n-1, -1, -1):
while stack and nums[stack[-1]] <= nums[i]:
stack.pop()
res[i] = 0 if not stack else (stack[-1] - i)
stack.append(i)
return res
T = [73,74,75,71,69,76]
res = daily_temperatures(T)
print(res)