The algorithm: Use two stacks: one for values, one for current minimums. Push: - Push value to value stack - Push min(value, current min) to min stack Pop: - Pop from both stacks Top: Return top of value stack GetMin: Return top of min stack The min stack always has the same height as the value stack. Each entry represents the minimum up to that point.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
$ curl repovive.com/roadmaps/maang-interview-prep/stacks/min-stack-solution
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░███████████████████████████████████████████████████████████████████████████