Skip to content

Commit 3b4e17f

Browse files
author
Partho Biswas
committed
946. Validate Stack Sequences
1 parent e486c44 commit 3b4e17f

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ I have solved quite a number of problems from several topics. See the below tabl
257257
|09| **[173. Binary Search Tree Iterator](https://tinyurl.com/wp2o8h2)** | [Python](https://tinyurl.com/wu6rdaw/173_Binary_Search_Tree_Iterator.py), [Swift](https://tinyurl.com/wuja3c4/173_Binary_Search_Tree_Iterator.swift)| [Vid 1](https://tinyurl.com/vb62v3q), **[Art 1](https://tinyurl.com/smu9ku3)** | Medium | TODO: Check again. Very Important. Learned new things |
258258
|10| **[284. Peeking Iterator](https://tinyurl.com/wp2o8h2)** | [Python](https://tinyurl.com/wu6rdaw/284_Peeking_Iterator.py), [Swift](https://tinyurl.com/wuja3c4/284_Peeking_Iterator.swift)| **[Art 1](https://tinyurl.com/wv4ufcz)** | Medium | --- |
259259
|11| **[281. Zigzag Iterator](https://tinyurl.com/rxgn345)** | [Python](https://tinyurl.com/wu6rdaw/281_Zigzag_Iterator.py), [Swift](https://tinyurl.com/wuja3c4/281_Zigzag_Iterator.swift)| **[Art 1](https://tinyurl.com/wv4ufcz)** | Medium | --- |
260+
|12| **[946. Validate Stack Sequences](https://tinyurl.com/u8wbaqp)** | [Python](https://tinyurl.com/wu6rdaw/946_Validate_Stack_Sequences.py), [Swift](https://tinyurl.com/wuja3c4/946_Validate_Stack_Sequences.swift)| **[Art 1](https://tinyurl.com/um2r3bf)**, **[Art 2](https://tinyurl.com/vc5wmlj)** | Medium | --- |
260261

261262
</p>
262263
</details>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Time and space both O(n)
2+
class Solution(object):
3+
def validateStackSequences(self, pushed, popped):
4+
"""
5+
:type pushed: List[int]
6+
:type popped: List[int]
7+
:rtype: bool
8+
"""
9+
stack = []
10+
for i in range(len(pushed)):
11+
stack.append(pushed[i])
12+
while stack and popped and popped[0] == stack[-1]:
13+
stack.pop()
14+
popped.pop(0)
15+
return len(stack) == 0
16+
17+
18+
19+
20+
21+
# Time : :(n) | Space: O(1)
22+
class Solution(object):
23+
def validateStackSequences(self, pushed, popped):
24+
"""
25+
:type pushed: List[int]
26+
:type popped: List[int]
27+
:rtype: bool
28+
"""
29+

0 commit comments

Comments
 (0)