-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvalidate-stack-sequences.py
65 lines (58 loc) · 2.05 KB
/
validate-stack-sequences.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 946. Validate Stack Sequences
# 🟠 Medium
#
# https://leetcode.com/problems/validate-stack-sequences/
#
# Tags: Array - Stack - Simulation
import timeit
from typing import List
# Use an extra stack of memory and simulate the operations that took
# place, push the next element, then, while the top of the stack matches
# the next element that needs to be popped, pop it.
#
# Time complexity: O(n) - We visit each element on the pushed array and
# do amortized O(1) for each.
# Space complexity: O(n) - The stack can grow to the same size as the
# input.
#
# Runtime 57 ms Beats 99.86%
# Memory 14.1 MB Beats 85.31%
class Solution:
def validateStackSequences(
self, pushed: List[int], popped: List[int]
) -> bool:
# Try to simulate the stack, we pop when we can and push
# when we cannot pop.
n, stack, next_pop = len(pushed), [], 0
for el in pushed:
# Push the next element.
stack.append(el)
# Pop all elements that match.
while stack and next_pop < n and stack[-1] == popped[next_pop]:
stack.pop()
next_pop += 1
# If the sequence is valid, we would have popped everything.
return next_pop == n
def test():
executors = [Solution]
tests = [
[[1, 2, 3, 4, 5], [4, 5, 3, 2, 1], True],
[[1, 2, 3, 4, 5], [4, 3, 5, 1, 2], False],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
result = sol.validateStackSequences(t[0], t[1])
exp = t[2]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()