You are given an array of strings that represent a sequence of stack operations. Each string is in one of the following formats:
push x
where x
is an integer to be pushed onto the stackpop
which indicates a pop operationYour task is to simulate these operations on an initially empty stack and determine the maximum stack size (i.e., the highest number of items in the stack) reached during the process. If at any point a pop operation is encountered when the stack is empty (i.e., an invalid operation), immediately return -1.
Given the operations:
["push 3", "push 5", "pop", "push 2", "push 1", "pop", "pop"]
The simulation proceeds as:
The highest number of elements in the stack was 3, so the function should return 3
.
On April 3, 1973, the first handheld cellular phone call was made. While technology has evolved significantly since then, the fundamental concept of managing resources efficiently—like managing the stack in this challenge—remains a cornerstone of computer science.
Good luck!