Super Stack Operations
MediumStacks · Atlassian
Simulate a stack of integers that supports one extra operation. Each element of operations is a command string with space-separated parts:
"push v": push the integervonto the top of the stack."pop": remove the top element. Popping an empty stack does nothing."inc k d": add the integerdto each of the bottomkelements (thekelements pushed earliest that are still on the stack). If the stack holds fewer thankelements, adddto all of them. Ifkis0or the stack is empty, nothing changes.
After every operation, record the value currently on top of the stack as an integer, or the string "EMPTY" if the stack is empty. Return the list of recorded values, one per operation.
Example 1
- Input:
- operations = ["push 4", "push 5", "inc 2 1", "pop"]
- Output:
- [4, 5, 6, 5]
- Explanation
- The pushes report 4 and then 5. The increment adds 1 to both elements, making the stack 5, 6, so the top reads 6. Popping it leaves 5 on top.
Example 2
- Input:
- operations = ["push 3", "push -2", "push 7", "inc 2 10", "pop", "pop", "inc 5 -1", "pop", "pop"]
- Output:
- [3, -2, 7, 7, 8, 13, 12, "EMPTY", "EMPTY"]
- Explanation
inc 2 10changes only the bottom two elements, so the stack becomes 13, 8, 7 and the top still reads 7. The pops then reveal 8 and 13.inc 5 -1covers the single remaining element, giving 12. The last pop empties the stack, and popping an empty stack leaves it empty.
Example 3
- Input:
- operations = ["pop", "inc 3 2"]
- Output:
- ["EMPTY", "EMPTY"]
- Explanation
- Popping an empty stack does nothing, and an increment with no elements has nothing to change, so both report
"EMPTY".
Constraints
0 <= operations.length <= 10^5-10^9 <= v, d <= 10^90 <= k <= 10^5- Every command is well formed.