package Week_01; import java.lang.reflect.Method; public class MinStack155 { public static void main(String[] args) { MinStack s = new MinStack(); s.push(3); System.out.println(s.top() + " " + s.getMin()); s.push(2); System.out.println(s.top() + " " + s.getMin()); s.push(4); System.out.println(s.top() + " " + s.getMin()); s.push(1); System.out.println(s.top() + " " + s.getMin()); s.pop(); } } class MinStack { int[] stack; int[] minStack; int size; int length; /** initialize your data structure here. */ public MinStack() { int initLength = 10; this.size = 0; this.length = initLength; this.stack = new int[initLength]; this.minStack = new int[initLength]; } public void isFull() { if (this.length > this.size) { return; } int incr = 5; int[] newStack = new int[this.length + incr]; int[] newMinStack = new int[this.length + incr]; System.arraycopy(this.stack, 0, newStack, 0, this.length); System.arraycopy(this.minStack, 0, newMinStack, 0, this.length); this.stack = newStack; this.minStack = newMinStack; this.length += incr; } public boolean isEmpty() { return this.size == 0; } public void push(int val) { this.isFull(); this.stack[this.size] = val; if (this.isEmpty()) { this.minStack[this.size] = val; } else { this.minStack[this.size] = Math.min(this.minStack[this.size - 1], val); } this.size++; } public void pop() { if (this.isEmpty()) { return; } this.stack[this.size-1] = 0; this.minStack[this.size-1] = 0; this.size--; } public int top() { if (this.isEmpty()) { return 0; } return this.stack[this.size-1]; } public int getMin() { if (this.isEmpty()) { return 0; } return this.minStack[this.size-1]; } } /** * Your MinStack object will be instantiated and called as such: * MinStack obj = new MinStack(); * obj.push(val); * obj.pop(); * int param_3 = obj.top(); * int param_4 = obj.getMin(); */