Maintain an auxiliary stack (or vector) to keep track of current minimums as values are pushed and popped.
getMin()can return in O(1) time by tracking minimums separately.- Each time a value ≤ current min is pushed, also push it to
_min. - When popping, compare with
_min.top()to keep_minin sync. - Retained
stackimplementation for simplicity and STL alignment. - Commented out
vectorversion remains in header for optional optimization. - Runtime: Stack-based version beats ~45% of submissions — not top-tier, but acceptable given simplicity.
- Implementation Choice:
- Although
vectorversion is also implemented (and usually performs slightly better in practice),
thestackversion is retained as the active one to keep the code consistent with STL semantics and simplify testing. - The
vectorimplementation is preserved in comments for easy benchmarking or future optimization.
- Although