-
Notifications
You must be signed in to change notification settings - Fork 0
/
Min_stack.java
119 lines (102 loc) · 2.31 KB
/
Min_stack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.util.Stack;
//Min_stack
// using two stacks to implement a minstack
// the first stack is to store numbers, the other one is to store the min number from 1st stack
public class minstack{
private Stack<Integer> stack1;
private Stack<Integer> min_stack;
// copy constructor
public minstack(){
stack1 = new Stack<Integer>();
min_stack = new Stack<Integer>();
}
public void push(int number){
stack1.push(number);
if(min_stack.empty()){
min_stack.push(number);
}else{
if(min_stack.peek() >= number){
min_stack.push(number);
}
}
}
public int pop(){
if(stack1.peek() == min_stack.peek()){
min_stack.pop();
}
return stack1.pop();
}
public int min(){
return min_stack.peek();
}
public static void main(String[] args){
minstack minstack = new minstack();
minstack.push(2);
minstack.push(4);
minstack.push(10);
minstack.push(3);
minstack.pop();
System.out.println(minstack.min());
}
}
// solution 2
private Stack<Integer> stack = new Stack<Integer>();
private Stack<Integer> minStack = new Stack<Integer>();
public void push(int x) {
stack.push(x);
if(minStack.isEmpty() || x <= minStack.peek()){
minStack.push(x);
}
}
public void pop() {
if(stack.isEmpty()){
return;
}
int x = stack.peek();
if(!minStack.isEmpty() && x == minStack.peek()){
minStack.pop();
}
stack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
if(minStack.isEmpty()){
return 0;
}
return minStack.peek();
}
// solution 3, only use one stack
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
}
public int top() {
long top=stack.peek();
if (top>0){
return (int)(top+min);
}else{
return (int)(min);
}
}
public int getMin() {
return (int)min;
}
}