Skip to content
CodeItRaw
Code Reading
/
Monotonic Stack
Path
Play
Daily
Leaderboard
tr
Predict the Output
Monotonic Stack
Each new number pops the smaller ones off the stack. What is printed at the end?
1
stack = []
2
for
x
in
[
3
,
1
,
4
,
1
,
5
]:
3
while
stack
and
stack[-
1
] < x:
4
stack.pop()
5
stack.append(x)
6
print
(stack)