Implement a stack that supports retrieving the minimum element in constant time.
Your task is to design a data structure, MinStack
, that supports the following operations:
All operations should run in constant time, O(1).
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); // Returns -3.
minStack.pop();
minStack.top(); // Returns 0.
minStack.getMin(); // Returns -2.
Hint: Consider using an auxiliary stack to keep track of minimum values.
On February 6, we celebrate persistence and innovation in technology. Use this challenge as your building block to stack up your coding skills!