-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap.java
More file actions
241 lines (175 loc) · 5.7 KB
/
Copy pathHeap.java
File metadata and controls
241 lines (175 loc) · 5.7 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import java.util.*;
class Pair{
int first;
String second;
Pair(int f,String s){
first = f;
second = s;
}
@Override
public String toString() {
return "(" + first + ", " + second + ")";
}
}
class Pair2{
int first;
int second;
Pair2(int f,int s){
first = f;
second = s;
}
}
public class Heap {
public int kthSmallest(int[] arr, int k) {
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
// Insert first k elements
for (int i = 0; i < k; i++) {
pq.add(arr[i]);
}
// Process remaining elements
for (int i = k; i < arr.length; i++) {
if (arr[i] < pq.peek()) {
pq.poll();
pq.add(arr[i]);
}
}
return pq.peek();
}
public int findKthLargest(int[] arr, int k) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
// Insert first k elements
for (int i = 0; i < k; i++) {
pq.add(arr[i]);
}
// Process remaining elements
for (int i = k; i < arr.length; i++) {
if (arr[i] > pq.peek()) {
pq.poll();
pq.add(arr[i]);
}
}
return pq.peek();
}
public static int[] topKFrequent(int[] nums, int k){
PriorityQueue<Pair2> pq = new PriorityQueue<>(
(a,b) -> {
if(a.second != b.second){
return b.second - a.second;
}
return b.first - a.first;
}
);
HashMap<Integer,Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
int a = entry.getKey();
int b = entry.getValue();
pq.offer(new Pair2(a,b));
}
int[] arr = new int[k];
for(int i = 0;i < k;i++){
arr[i] = pq.poll().first;
}
return arr;
}
public static int[] topKFrequentOptimal(int[] nums, int k) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
// Min Heap based on frequency
PriorityQueue<Pair2> pq = new PriorityQueue<>(
(a, b) -> {
if (a.second != b.second) {
return a.second - b.second;
}
return a.first - b.first;
}
);
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
int element = entry.getKey();
int freq = entry.getValue();
Pair2 curr = new Pair2(element, freq);
if (pq.size() < k) {
pq.offer(curr);
continue;
}
if (curr.second > pq.peek().second) {
pq.poll();
pq.offer(curr);
}
}
int[] ans = new int[k];
int i = 0;
while (!pq.isEmpty()) {
ans[i++] = pq.poll().first;
}
return ans;
}
public static void main(String[] args) {
// Heap heap = new Heap();
// int[] arr = {7, 10, 4, 3, 20, 15};
// int k = 3;
// System.out.println("Kth smallest element is: " + heap.kthSmallest(arr, k));
//Min Heap on First, Min Heap in Second
System.out.println("Min Heap on First, Min Heap in Second");
PriorityQueue<Pair> pq1 = new PriorityQueue<>(
(a,b) -> {
if(a.first != b.first){
return a.first - b.first;
}
return a.second.compareTo(b.second);
}
);
pq1.add(new Pair(1,"apple"));
pq1.add(new Pair(2, "banana"));
pq1.add(new Pair(1,"guava"));
pq1.add(new Pair(1,"aardvark"));
while (!pq1.isEmpty())
System.out.println(pq1.poll());
System.out.println("Min Heap on First, Max Heap on Second");
PriorityQueue<Pair> pq2 = new PriorityQueue<>(
(a, b) -> {
if (a.first != b.first)
return a.first - b.first;
return b.second.compareTo(a.second);
}
);
pq2.add(new Pair(1, "apple"));
pq2.add(new Pair(2, "banana"));
pq2.add(new Pair(1, "guava"));
pq2.add(new Pair(1, "aardvark"));
while (!pq2.isEmpty())
System.out.println(pq2.poll());
System.out.println("Max Heap on First, Min Heap on Second");
PriorityQueue<Pair> pq3 = new PriorityQueue<>(
(a, b) -> {
if (a.first != b.first)
return b.first - a.first;
return a.second.compareTo(b.second);
}
);
pq3.add(new Pair(1, "apple"));
pq3.add(new Pair(2, "banana"));
pq3.add(new Pair(1, "guava"));
pq3.add(new Pair(1, "aardvark"));
while (!pq3.isEmpty())
System.out.println(pq3.poll());
System.out.println("Max Heap on First, Max Heap on Second");
PriorityQueue<Pair> pq4 = new PriorityQueue<>(
(a, b) -> {
if (a.first != b.first)
return b.first - a.first;
return b.second.compareTo(a.second);
}
);
pq4.add(new Pair(1, "apple"));
pq4.add(new Pair(2, "banana"));
pq4.add(new Pair(1, "guava"));
pq4.add(new Pair(1, "aardvark"));
while (!pq4.isEmpty())
System.out.println(pq4.poll());
}
}