- Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathMinStack.java
More file actions
Latest commit
157 lines (128 loc) · 3.08 KB
/
Copy pathMinStack.java
File metadata and controls
157 lines (128 loc) · 3.08 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
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.
*
*/
importjava.util.SortedMap;
importjava.util.TreeMap;
classMinStack {
SortedMap<Integer, Integer> map = newTreeMap<>();;
Stack<Integer> st = newStack<>();
/** initialize your data structure here. */
publicMinStack() { }
publicvoidpush(intx) {
if (map.containsKey(x)) {
map.put(x, map.get(x)+1);
} else {
map.put(x, 1);
}
st.push(x);
}
publicvoidpop() {
IntegertopEle = st.peek();
intcount = map.get(topEle);
count--;
if (count <= 0) {
map.remove(topEle);
} else {
map.put(topEle, count);
}
st.pop();
}
publicinttop() {
returnst.peek();
}
publicintgetMin() {
returnmap.firstKey();
}
}
/**
* https://discuss.leetcode.com/topic/4953/share-my-java-solution-with-only-one-stack
*/
classMinStack2 {
staticclassElement
{
finalintvalue;
finalintmin;
Element(finalintvalue, finalintmin)
{
this.value = value;
this.min = min;
}
}
finalStack<Element> stack = newStack<>();
publicvoidpush(intx) {
finalintmin = (stack.empty()) ? x : Math.min(stack.peek().min, x);
stack.push(newElement(x, min));
}
publicvoidpop()
{
stack.pop();
}
publicinttop()
{
returnstack.peek().value;
}
publicintgetMin()
{
returnstack.peek().min;
}
}
/**
* https://discuss.leetcode.com/topic/4953/share-my-java-solution-with-only-one-stack
*/
classMinStack3 {
longmin;
Stack<Long> stack;
publicMinStack(){
stack=newStack<>();
}
publicvoidpush(intx) {
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;
}
}
publicvoidpop() {
if (stack.isEmpty()) return;
longpop=stack.pop();
if (pop<0) min=min-pop;//If negative, increase the min value
}
publicinttop() {
longtop=stack.peek();
if (top>0){
return (int)(top+min);
}else{
return (int)(min);
}
}
publicintgetMin() {
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();
*/