946.js
/** ------------------------------------------------------------------------------
*
* 2023-04-13
* 946. Validate Stack Sequences
* https://leetcode.com/problems/validate-stack-sequences/description/
*
------------------------------------------------------------------------------ */
var validateStackSequences = function (pushed, popped) {
const stack = []
let i = 0
for (const num of pushed) {
stack.push(num)
while (stack.length > 0 && stack[stack.length - 1] === popped[i]) {
stack.pop()
i++
}
}
return stack.length === 0
}
console.log(validateStackSequences([1, 2, 3, 4, 5], [4, 5, 3, 2, 1]))
console.log(validateStackSequences([1, 2, 3, 4, 5], [4, 3, 5, 1, 2]))