946 Validate Stack Sequences
1. Question
Given two sequencespushed
andpopped
with distinct values, returntrue
if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack.
Example 1:
Example 2:
Note:
0 <= pushed.length == popped.length <= 1000
0 <= pushed[i], popped[i] < 1000
pushed
is a permutation ofpopped
.pushed
andpopped
have distinct values.
2. Implementation
(1) Stack
3. Time & Space Complexity
Stack: Time: O(n), Space: O(n)
Last updated
Was this helpful?