CJCoding With Joseph
15free / daySign inCreate account

Track the Minimum With a Second Stack

MinStack keeps two stacks: main holds every value, and mins holds only the values that were the smallest so far, so getMin() can just read the top of mins. Your push and pop must keep both in step.

Requirements:
1. push(int value) puts the value on main, and also on mins when mins is empty or value is less than or equal to the current minimum.
2. pop() removes and returns the top of main, and removes the top of mins as well when the two are equal.
3. pop() returns -1 on an empty stack. getMin() is already written and returns -1 when there is no minimum.

Do NOT write a main method.

Expected Output:

-1
5
3
3
7
3
3
5
1

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Data Structures & AlgorithmsStacks
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (sysout, psvm, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.