-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinStack.java
More file actions
160 lines (113 loc) · 3.66 KB
/
MinStack.java
File metadata and controls
160 lines (113 loc) · 3.66 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
158
159
package Algorithms.StackAlgos;
import java.util.*;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 02 March 2025
* @link 155. Min Stack <a href = "https://leetcode.com/problems/min-stack"> LeetCode Link </a>
* @topics Array, Stack, Design
*/
public class MinStack {
public static void main(String[] args) {
MinStackUsingIntArray minStack = new MinStackUsingIntArray();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
System.out.println(minStack.getMin());
minStack.pop();
System.out.println(minStack.top());
System.out.println(minStack.getMin());
}
static class MinStackUsingIntArray {
Stack<int[]> stack;
public MinStackUsingIntArray() {
stack = new Stack<>();
}
public void push(int val) {
stack.push(new int[]{val, stack.isEmpty()? val : Math.min(stack.peek()[1], val)}); // or Math.min((stack.isEmpty()? val : stack.peek()[1]), val)
}
public void pop() {
stack.pop();
}
public int top() {
return stack.peek()[0];
}
public int getMin() {
return stack.peek()[1];
}
}
/**
* Maintain the min values of the original stack at that specific point of push() method in a separate stack -- minStack
*/
static class MinStackUsingExtraMinStack {
Stack<Integer> stack = new Stack<>(); // k=number, v=minAtThatPoint
Stack<Integer> minStack = new Stack<>(); // k=number, v=minAtThatPoint
public void push(int val) {
stack.push(val);
minStack.push(Math.min(minStack.isEmpty()? val: minStack.peek(), val));
}
public void pop() {
stack.pop();
minStack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}
static class MinStackUsingAbstractMap {
Stack<Map.Entry<Integer,Integer>> stack = new Stack<>(); // k=number, v=minAtThatPoint
public void push(int val) {
int minVal = stack.isEmpty()? val: stack.peek().getValue();
minVal = Math.min(minVal, val); //check if curr val is min or not
stack.push(new AbstractMap.SimpleEntry<>(val, minVal));
}
public void pop() {
stack.pop();
}
public int top() {
return stack.peek().getKey();
}
public int getMin() {
return stack.peek().getValue();
}
}
static class MinStackUsingTreeMap {
TreeMap<Integer, Integer> map = new TreeMap<>();
Stack<Integer> stack = new Stack<>();
public void push(int val) {
stack.push(val);
map.merge(val, 1, Integer::sum);
}
public void pop() {
int num = stack.pop();
if (map.get(num)==1) map.remove(num);
else map.put(num, map.get(num)-1);
}
public int top() {
return stack.peek();
}
public int getMin() {
return map.firstKey();
}
}
static class MinStackUsingMinHeap {
PriorityQueue<Integer> pq=new PriorityQueue<>();
Stack<Integer> stk=new Stack<>();
public void push(int val) {
pq.add(val);
stk.push(val);
}
public void pop() {
pq.remove(stk.peek());
stk.pop();
}
public int top() {
return stk.peek();
}
public int getMin() {
return pq.isEmpty()? Integer.MAX_VALUE : pq.peek();
}
}
}