Package g0101_0200.s0155_min_stack
Class MinStack
java.lang.Object
g0101_0200.s0155_min_stack.MinStack
155 - Min Stack.<p>Easy</p>
<p>Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.</p>
<p>Implement the <code>MinStack</code> class:</p>
<ul>
<li><code>MinStack()</code> initializes the stack object.</li>
<li><code>void push(int val)</code> pushes the element <code>val</code> onto the stack.</li>
<li><code>void pop()</code> removes the element on the top of the stack.</li>
<li><code>int top()</code> gets the top element of the stack.</li>
<li><code>int getMin()</code> retrieves the minimum element in the stack.</li>
</ul>
<p><strong>Example 1:</strong></p>
<p><strong>Input</strong></p>
<pre><code> ["MinStack","push","push","push","getMin","pop","top","getMin"]
[ [],[-2],[0],[-3],[],[],[],[]]
</code></pre>
<p><strong>Output:</strong> [null,null,null,null,-3,null,0,-2]</p>
<p><strong>Explanation:</strong></p>
<pre><code> MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); // return -3
minStack.pop();
minStack.top(); // return 0
minStack.getMin(); // return -2
</code></pre>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= val <= 2<sup>31</sup> - 1</code></li>
<li>Methods <code>pop</code>, <code>top</code> and <code>getMin</code> operations will always be called on <strong>non-empty</strong> stacks.</li>
<li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>push</code>, <code>pop</code>, <code>top</code>, and <code>getMin</code>.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
MinStack
public MinStack()
-
-
Method Details
-
push
public void push(int val) -
pop
public void pop() -
top
public int top() -
getMin
public int getMin()
-