You are given a list of operations that simulate pushing and popping elements on a stack. Your task is to determine the maximum number of elements that were present on the stack at any point during these operations.
push X
, where X
is a value (the value itself does not affect the result).pop
.If a pop operation is performed when the stack is empty, ignore it.
Input: ["push 1", "push 2", "pop", "push 3", "push 4", "pop"]
Simulation:
push 1
→ Stack: [1] → Max size: 1push 2
→ Stack: [1, 2] → Max size: 2pop
→ Stack: [1] → Max size: 2push 3
→ Stack: [1, 3] → Max size: 2push 4
→ Stack: [1, 3, 4] → Max size: 3pop
→ Stack: [1, 3] → Max size: 3Output: 3
Implement a function named maxStackDepth
that accepts a list (or array) of strings representing the operations and returns an integer that indicates the maximum size the stack reached during the operations.
Remember: Effective use of stacks is crucial in many programming tasks. Enjoy coding and have fun solving this problem on this fine January 10th!