Implement Stack Using Array
Implement a stack using an array with the following operations:
push(x): pushxonto the stack.pop(): remove the element on top of the stack.top(): read the top element.empty(): report whether the stack is empty.
You are given two parallel arrays, operations and values, of equal length. Execute the operations in order against a single stack. The two arrays are aligned by index: values[i] is the argument for operations[i]. When operations[i] is "push", values[i] is the value pushed. For every other operation, values[i] is ignored; a placeholder 0 is used by convention.
Return one integer array holding the result of every top() and empty() call, in order. top() contributes the current top value, or -1 when the stack is empty. empty() contributes 1 when the stack is empty and 0 otherwise. push and pop contribute nothing.
Examples
Input: [["push","top","pop"],[1,0,0]]
Output: [1]
Input: [["push","push","top","pop","top","pop"],[1,2,0,0,0,0]]
Output: [2,1]
Hints
Keep the top of the stack at the end of an array so `push` and `pop` never shift other elements.
Walk both arrays with the same index `i`; read `values[i]` only when `operations[i]` is `"push"`.
For `empty()`, append `1` when the stack length is zero and `0` otherwise.
Related Problems
Implement Stack Using Array
Implement a stack using an array with the following operations: