Skip to content

Commit cda7a91

Browse files
committed
Update 946
1 parent dd9900b commit cda7a91

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/946.validate-stack-sequences.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
# @lc code=start
88
# TAGS: Stack
99
class Solution:
10-
# 68 ms, 85.45%. Time and Space: O(N)
10+
# 64 ms, 94.09%. Time and Space: O(N)
1111
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
12-
i = j = 0
12+
ptr = 0
1313
stack = []
14-
while i < len(pushed):
15-
stack.append(pushed[i])
16-
while stack and j < len(popped) and stack[-1] == popped[j]:
14+
for val in pushed:
15+
stack.append(val)
16+
while stack and stack[-1] == popped[ptr]:
17+
ptr += 1
1718
stack.pop()
18-
j += 1
19-
i += 1
20-
return not stack
19+
return stack == []
20+
2121
# @lc code=end
2222

0 commit comments

Comments
 (0)