-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-155-Min-Stack.java
More file actions
141 lines (113 loc) · 3.68 KB
/
LeetCode-155-Min-Stack.java
File metadata and controls
141 lines (113 loc) · 3.68 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
LeetCode: https://leetcode.com/problems/min-stack/
LintCode: http://www.lintcode.com/problem/min-stack/
JiuZhang: http://www.jiuzhang.com/solutions/min-stack/
ProgramCreek: http://www.programcreek.com/2014/02/leetcode-min-stack-java/
GeeksForGeeks: http://www.geeksforgeeks.org/design-and-implement-special-stack-data-structure/
Analysis:
Use two stacks, one for actual values, another for mininum values.
The idea is to do push and pop operation for top of minStack always be minimum.
Push(int x) // inserts an element x to Special Stack
1) push x to the first stack (the stack with actual elements)
2) compare x with the top element of the second stack (the auxiliary stack). Let the top element be y.
…..a) If x is smaller than y then push x to the auxiliary stack.
…..b) If x is greater than y then push y to the auxiliary stack.
int Pop() // removes an element from Special Stack and return the removed element
1) pop the top element from the auxiliary stack.
2) pop the top element from the actual stack and return it.
The step 1 is necessary to make sure that the auxiliary stack is also updated for future operations.
int getMin() // returns the minimum element from Special Stack
1) Return the top element of auxiliary stack.
All above operation is O(1)
*/
// 1.Two stacks.
// class MinStack {
// private Stack<Integer> stack;
// private Stack<Integer> minStack;
// public MinStack(){
// this.stack = new Stack<Integer>();
// this.minStack = new Stack<Integer>();
// }
// public void push(int x) {
// stack.push(x);
// if(minStack.isEmpty()){
// minStack.push(x);
// }else{
// minStack.push(Math.min(x, minStack.peek()));
// }
// }
// public void pop() {
// minStack.pop();
// stack.pop();
// }
// public int top() {
// return stack.peek();
// }
// public int getMin() {
// return minStack.peek();
// }
// }
// 2.Two stacks. This one will save a little space. Time is also O(1)
class MinStack {
private Stack<Integer> stack;
private Stack<Integer> minStack;
public MinStack(){
this.stack = new Stack<Integer>();
this.minStack = new Stack<Integer>();
}
public void push(int x){
stack.push(x);
if(minStack.isEmpty()){
minStack.push(x);
}else{
if(minStack.peek() >= x) minStack.push(x);
}
}
public void pop(){
// if(stack.peek() == minStack.peek())minStack.pop(); // this is wrong because you cannot compare two different elements (Integer) using == in Java.
if (stack.peek().equals(minStack.peek())) minStack.pop();
stack.pop();
}
public int top(){
return stack.peek();
}
public int getMin(){
return minStack.peek();
}
}
// 3.Using a self-defined inner class
/*
Runtime: 47 ms, faster than 69.03% of Java online submissions for Min Stack.
*/
class MinStack {
private class Pair {
int val;
int min;
public Pair(int val, int min) {
this.val = val;
this.min = min;
}
}
Stack<Pair> stack;
/** initialize your data structure here. */
public MinStack() {
stack = new Stack<>();
}
public void push(int x) {
int min = x;
if (!stack.isEmpty()) {
min = Math.min(min, stack.peek().min);
}
Pair p = new Pair(x, min);
stack.push(p);
}
public void pop() {
stack.pop();
}
public int top() {
return stack.peek().val;
}
public int getMin() {
return stack.peek().min;
}
}