# Min Stack LeetCode Solution

Here, We see Min Stack LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

# List of all LeetCode Solution

## Problem Statement

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

Implement the MinStack class:

• MinStack() initializes the stack object.
• void push(int val) pushes the element val onto the stack.
• void pop() removes the element on the top of the stack.
• int top() gets the top element of the stack.
• int getMin() retrieves the minimum element in the stack.
```Example 1:

Input:
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]

Output:
[null,null,null,null,-3,null,0,-2]

Explanation:
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```

## Min Stack Leetcode Solution C++

```.wp-block-code {
border: 0;
}

.wp-block-code > span {
display: block;
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```class MinStack {
public:
void push(int x) {
if (stack.empty())
stack.emplace(x, x);
else
stack.emplace(x, min(x, stack.top().second));
}
void pop() {
stack.pop();
}
int top() {
return stack.top().first;
}
int getMin() {
return stack.top().second;
}
private:
stack<pair<int, int>> stack;  // {x, min}
};
```Code language: C++ (cpp)```

## Min Stack Leetcode Solution Java

``````class MinStack {
int min = Integer.MAX_VALUE;
Stack<Integer> stack = new Stack<Integer>();
public void push(int x) {
if(x <= min){
stack.push(min);
min=x;
}
stack.push(x);
}
public void pop() {
if(stack.pop() == min) min=stack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return min;
}
}
```Code language: Java (java)```

## Min Stack Leetcode Solution JavaScript

``````var MinStack = function() {
this.elements = [];
};
MinStack.prototype.push = function(x) {
this.elements.push({
value: x,
min: this.elements.length === 0 ? x : Math.min(x, this.getMin()),
});
};
MinStack.prototype.pop = function() {
this.elements.pop();
};
MinStack.prototype.top = function() {
return this.elements[this.elements.length - 1].value;
};
MinStack.prototype.getMin = function() {
return this.elements[this.elements.length - 1].min;
};
```Code language: JavaScript (javascript)```

## Min Stack Leetcode Solution Python

``````class MinStack:
def __init__(self):
self.stack= []
def push(self, val):
if not self.stack:self.stack.append((val,val))
else:
self.stack.append((val,min(val,self.stack[-1][1])))
def pop(self):
if self.stack: self.stack.pop()
def top(self):
if self.stack: return self.stack[-1][0]
else: return None
def getMin(self):
if self.stack: return self.stack[-1][1]
else: return None
```Code language: Python (python)```
Scroll to Top