Given two integer arrays pushed
and popped
each with distinct values, return true
if this could have been the result of a sequence of push and pop operations on an initially empty stack, or false
otherwise.
Example 1:
Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1] Output: true Explanation: We might do the following sequence: push(1), push(2), push(3), push(4), pop() -> 4, push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
Example 2:
Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2] Output: false Explanation: 1 cannot be popped before 2.
Constraints:
1 <= pushed.length <= 1000
0 <= pushed[i] <= 1000
pushed
are unique.popped.length == pushed.length
popped
is a permutation of pushed
.This problem asks whether two arrays, pushed
and popped
, represent a valid sequence of push and pop operations on a stack. The core idea is to simulate the stack operations using a stack data structure.
Algorithm:
Initialization: Create an empty stack stk
. Initialize an index i
to 0, pointing to the beginning of the popped
array. This index tracks which element of popped
we expect to encounter next.
Iteration: Iterate through the pushed
array. For each element x
in pushed
:
x
onto the stack stk
.popped[i]
, pop the top element from the stack and increment i
. This simulates popping elements from the stack as long as they match the expected sequence in popped
.Validation: After iterating through pushed
, if i
equals the length of popped
, it means we successfully popped all the elements in the correct order. Therefore, return true
. Otherwise, return false
.
Time Complexity: O(N) - We iterate through the pushed
array once, and the popping operation in the while loop takes at most O(N) time in the worst-case scenario (where all pushes are followed by pops). However, each element is pushed and popped at most once, resulting in a linear time complexity.
Space Complexity: O(N) - In the worst case, the stack stk
can hold all elements of pushed
if no pops happen before the end of the iteration.
Code Examples:
The provided code solutions in multiple languages effectively implement this algorithm. They all follow the same structure:
pushed
, pushing each element onto the stack.while
loop to simulate popping as long as the top of the stack matches the next expected element in popped
.i
(the index into popped
) has reached the end of popped
to determine validity.Example Walkthrough (Python):
Let's trace Example 1 (pushed
= [1,2,3,4,5], popped
= [4,5,3,2,1]):
stk
is empty, i
is 0.stk
= [1].stk
= [1, 2].stk
= [1, 2, 3].stk
= [1, 2, 3, 4]. The while loop executes: stk.top()
(4) == popped[i]
(4), so pop 4, stk
= [1, 2, 3], i
= 1.stk
= [1, 2, 3, 5]. The while loop executes: stk.top()
(5) == popped[i]
(5), so pop 5, stk
= [1, 2, 3], i
= 2.i
reaches 5 (the length of popped
), indicating a valid sequence.The other language examples follow the same logic using their respective stack implementations. The key is the efficient simulation of push and pop operations based on the input arrays.