Deviopath › JavaScript

Min Stack

Medium · Data Structures

Finish the MinStack class: a stack that can also report its smallest value instantly.

- push(value) adds a value on top.

- pop() removes and returns the top value, or null if the stack is empty.

- top() returns the top value without removing it, or null if empty.

- getMin() returns the smallest value in the stack, or null if empty.

Every method should be O(1): do not scan the whole stack in getMin. The function runMinStack is already written: it replays the operations and returns each result (null for push).

Next: Find the Bracket Error