Min-Tracking Stack
mediumA warehouse stacks crates, each stamped with a weight. The floor manager keeps asking, mid-shift: "what's the lightest crate currently in the stack?" — and wants the answer instantly, every time.
Design a stack supporting all of these in O(1):
push x— put crate of weightxon toppop— remove the top cratetop— report the top crate's weightget_min— report the lightest weight currently in the stack
Operations format. Your function receives one argument ops: a list of operations, each itself a list — ["push", x], ["pop"], ["top"], or ["get_min"]. Process them in order and return a list with one entry per operation: the returned value for top / get_min, and None for push / pop. All pop/top/get_min calls are guaranteed to occur on a non-empty stack.
Example 1
in ops = [["push", -2], ["push", 0], ["push", -3], ["get_min"], ["pop"], ["top"], ["get_min"]]
out [None, None, None, -3, None, 0, -2]
After popping -3, the lightest remaining crate is -2.
Example 2
in ops = [["push", 5], ["get_min"], ["push", 3], ["get_min"], ["pop"], ["get_min"]]
out [None, 5, None, 3, None, 5]
Constraints
- · 1 <= len(ops) <= 10^4
- · -10^9 <= x <= 10^9
- · pop, top and get_min are never called on an empty stack
- · every operation must run in O(1)
solution.py● loading python…
test results
Hit ▶ Run to test your code against the visible cases.