class MinStack(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
self.minStack = []
def push(self, x):
"""
:type x: int
:rtype: void
"""
self.stack.append(x)
if len(self.minStack) == 0 or x <= self.minStack[-1]: ## x less or equal to current minimum !!!!
self.minStack.append(x)
def pop(self):
"""
:rtype: void
"""
if self.stack[-1] == self.minStack[-1]:
self.minStack.pop()
return self.stack.pop()
def top(self):
"""
:rtype: int
"""
return self.stack[-1]
def getMin(self):
"""
:rtype: int
"""
return self.minStack[-1]
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()
public class MinStack {
/** initialize your data structure here. */
int min = Integer.MAX_VALUE;
Stack<Integer> stack;
public MinStack() {
stack = new Stack<>();
}
public void push(int x) {
if (x <= min){
stack.push(min); // maintain the minimum always be the top one
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;
}
}
/**
* 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();
*/
public class MinStack {
long min;
Stack<Long> stack;
public MinStack(){
stack=new Stack<>();
}
public void push(int x) {
if (stack.isEmpty()){
stack.push(0L);
min=x;
}else{
stack.push(x-min);//Could be negative if min value needs to change
if (x<min) min=x;
}
}
public void pop() {
if (stack.isEmpty()) return;
long pop=stack.pop();
if (pop<0) min=min-pop;//If negative, increase the min value
//pop <0 x-min<0, x<min-> min= x, 所以min是现在最小的数值,pop是现在这个数和之前的min差值,
//. However, the return value of pop function is void which means the value is not important. The top function can return the correct value.
}
public int top() {
long top=stack.peek();
if (top>0){
return (int)(top+min);
}else{
return (int)(min);
}
}
public int getMin() {
return (int)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();
*/
class MinStack {
private Node head;
public void push(int x) {
if(head == null)
head = new Node(x, x);
else
head = new Node(x, Math.min(x, head.min), head);
}
public void pop() {
head = head.next;
}
public int top() {
return head.val;
}
public int getMin() {
return head.min;
}
private class Node {
int val;
int min;
Node next;
private Node(int val, int min) {
this(val, min, null);
}
private Node(int val, int min, Node next) {
this.val = val;
this.min = min;
this.next = next;
}
}
}
public class MinStack {
private List<int[]> values;
//int curMin;
/** initialize your data structure here. */
public MinStack() {
values = new ArrayList<>();
// curMin = Integer.MAX_VALUE;
}
public void push(int x) {
// curMin = Math.min(x, curMin);
if (values.size() == 0) {
values.add(new int[] {x, x});
} else {
values.add(new int[] {x, Math.min(x, values.get(values.size() - 1)[1])}); //
}
}
public void pop() {
if (values.size() > 0) {
values.remove(values.size() - 1);
}
}
public int top() {
if (values.size() > 0) {
return values.get(values.size() - 1)[0];
}
return 0;
}
public int getMin() {
if (values.size() > 0) {
return values.get(values.size() - 1)[1];
}
return 0;
}
}
/**
* 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();
*/