Description
Given two integer sequences pushed and popped, return true if this could have been the result of push/pop operations on an initially empty stack.
Examples
Input:
pushed = [1,2,3,4,5], popped = [4,5,3,2,1]Output:
trueExplanation:
Valid sequence.
Input:
pushed = [1,2,3,4,5], popped = [4,3,5,1,2]Output:
falseExplanation:
For pushed = [1,2,3,4,5], popped = [4,3,5,1,2], the answer is false because the validate stack sequences condition is not met.
Input:
pushed = [1,2,3], popped = [3,1,2]Output:
falseExplanation:
After pushing 1,2,3 and popping 3, the stack contains [1,2]. To pop 1 next, the task requires pop 2 first, but the sequence expects 1 before 2, making this sequence invalid.
Constraints
- •
1 ≤ n ≤ 1000