forked from itcharge/AlgoNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack-MonotoneStack.py
More file actions
40 lines (35 loc) · 1.05 KB
/
Stack-MonotoneStack.py
File metadata and controls
40 lines (35 loc) · 1.05 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
import random
def monotoneStack(nums):
print(str(nums))
stack = []
for num in nums:
while stack and num <= stack[-1]:
top = stack[-1]
stack.pop()
print(str(top) + " 出栈 " + str(stack))
stack.append(num)
print(str(num) + " 入栈 " + str(stack))
def monotoneIncreasingStack(nums):
stack = []
for num in nums:
while stack and num >= stack[-1]:
top = stack[-1]
stack.pop()
print(str(top) + " 出栈 " + str(stack))
stack.append(num)
print(str(num) + " 入栈 " + str(stack))
def monotoneDecreasingStack(nums):
stack = []
for num in nums:
while stack and num <= stack[-1]:
top = stack[-1]
stack.pop()
print(str(top) + " 出栈 " + str(stack))
stack.append(num)
print(str(num) + " 入栈 " + str(stack))
nums = []
for i in range(8):
nums.append(random.randint(1, 9))
print(nums)
#nums = [4, 3, 2, 5, 7, 4, 6, 8]
monotoneIncreasingStack(nums)