Skip to content

Commit 926b996

Browse files
authored
Create validate-stack-sequences.py
1 parent 735e701 commit 926b996

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Python/validate-stack-sequences.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def validateStackSequences(self, pushed, popped):
6+
"""
7+
:type pushed: List[int]
8+
:type popped: List[int]
9+
:rtype: bool
10+
"""
11+
i = 0
12+
s = []
13+
for v in pushed:
14+
s.append(v)
15+
while s and i < len(popped) and s[-1] == popped[i]:
16+
s.pop()
17+
i += 1
18+
return i == len(popped)

0 commit comments

Comments
 (0)