-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathMinStack.java
157 lines (128 loc) · 3.08 KB
/
MinStack.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Design a stack that supports push, pop, top, and retrieving the minimum
* element in constant time.
*
* push(x) -- Push element x onto stack.
* pop() -- Removes the element on top of the stack.
* top() -- Get the top element.
* getMin() -- Retrieve the minimum element in the stack.
*
* Example:
* MinStack minStack = new MinStack();
* minStack.push(-2);
* minStack.push(0);
* minStack.push(-3);
* minStack.getMin(); --> Returns -3.
* minStack.pop();
* minStack.top(); --> Returns 0.
* minStack.getMin(); --> Returns -2.
*
*/
import java.util.SortedMap;
import java.util.TreeMap;
class MinStack {
SortedMap<Integer, Integer> map = new TreeMap<>();;
Stack<Integer> st = new Stack<>();
/** initialize your data structure here. */
public MinStack() { }
public void push(int x) {
if (map.containsKey(x)) {
map.put(x, map.get(x)+1);
} else {
map.put(x, 1);
}
st.push(x);
}
public void pop() {
Integer topEle = st.peek();
int count = map.get(topEle);
count--;
if (count <= 0) {
map.remove(topEle);
} else {
map.put(topEle, count);
}
st.pop();
}
public int top() {
return st.peek();
}
public int getMin() {
return map.firstKey();
}
}
/**
* https://discuss.leetcode.com/topic/4953/share-my-java-solution-with-only-one-stack
*/
class MinStack2 {
static class Element
{
final int value;
final int min;
Element(final int value, final int min)
{
this.value = value;
this.min = min;
}
}
final Stack<Element> stack = new Stack<>();
public void push(int x) {
final int min = (stack.empty()) ? x : Math.min(stack.peek().min, x);
stack.push(new Element(x, min));
}
public void pop()
{
stack.pop();
}
public int top()
{
return stack.peek().value;
}
public int getMin()
{
return stack.peek().min;
}
}
/**
* https://discuss.leetcode.com/topic/4953/share-my-java-solution-with-only-one-stack
*/
class MinStack3 {
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;
}
}
/**
* 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();
*/