/*
* @lc app=leetcode id=155 lang=javascript
*
* [155] Min Stack
*/
/**
* initialize your data structure here.
*/
var MinStack = function() {
this.stack = [];
this.minV = Number.MAX_VALUE;
};
/**
* @param {number} x
* @return {void}
*/
MinStack.prototype.push = function(x) {
// update 'min'
const minV = this.minV;
if (x < this.minV) {
this.minV = x;
}
return this.stack.push(x - minV);
};
/**
* @return {void}
*/
MinStack.prototype.pop = function() {
const item = this.stack.pop();
const minV = this.minV;
if (item < 0) {
this.minV = minV - item;
return minV;
}
return item + minV;
};
/**
* @return {number}
*/
MinStack.prototype.top = function() {
const item = this.stack[this.stack.length - 1];
const minV = this.minV;
if (item < 0) {
return minV;
}
return item + minV;
};
/**
* @return {number}
*/
MinStack.prototype.min = function() {
return this.minV;
};
/**
* Your MinStack object will be instantiated and called as such:
* var obj = new MinStack()
* obj.push(x)
* obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.min()
*/
C++ Code:
class MinStack {
stack<long> data;
long min = INT_MAX;
public:
/** initialize your data structure here. */
MinStack() {
}
void push(int x) {
data.push(x - min);
if(x < min)
{
min = x;
}
}
void pop() {
long top = data.top();
data.pop();
// 更新最小值
if(top < 0)
{
min -= top;
}
}
int top() {
long top = data.top();
// 最小值为 min
if (top < 0)
{
return min;
}
else{
return min+top;
}
}
int getMin() {
return min;
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
Java Code:
class MinStack {
long min;
Stack<Long> stack;
/** initialize your data structure here. */
public MinStack() {
stack = new Stack<>();
}
public void push(int x) {
if (stack.isEmpty()) {
stack.push(0L);
min = x;
}
else {
stack.push(x - min);
if (x < min)
min = x;
}
}
public void pop() {
long p = stack.pop();
if (p < 0) {
// if (p < 0), the popped value is the min
// Recall p is added by this statement: stack.push(x - min);
// So, p = x - old_min
// old_min = x - p
// again, if (p < 0), x is the min so:
// old_min = min - p
min = min - p;
}
}
public int top() {
long p = stack.peek();
if (p < 0) {
return (int) min;
}
else {
// p = x - min
// x = p + min
return (int) (p + min);
}
}
public int getMin() {
return (int) min;
}
}
Python Code:
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.minV = float('inf')
self.stack = []
def push(self, x: int) -> None:
self.stack.append(x - self.minV)
if x < self.minV:
self.minV = x
def pop(self) -> None:
if not self.stack:
return
tmp = self.stack.pop()
if tmp < 0:
self.minV -= tmp
def top(self) -> int:
if not self.stack:
return
tmp = self.stack[-1]
if tmp < 0:
return self.minV
else:
return self.minV + tmp
def min(self) -> int:
return self.minV
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.min()