-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindMedianFromDataStream.java
More file actions
266 lines (188 loc) · 7.24 KB
/
FindMedianFromDataStream.java
File metadata and controls
266 lines (188 loc) · 7.24 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package Algorithms.HeapAlgos;
import java.util.*;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 23 April 2026
* @link 295. Find Median from Data Stream <a href="https://leetcode.com/problems/find-median-from-data-stream/">LeetCode Link</a>
* @topics Two Pointers, Design, Array, Sorting, Heap(PriorityQueue), Data Stream
* @companies Amazon(7), Microsoft(6), IXL(3), Google(5), Citadel(5), Apple(4), PayPal(3), Meta(2), Bloomberg(2), Tinder(2), Oracle(12), Pinterest(10), Anduril(9), TikTok(7), Goldman Sachs(6), Uber(6), Flipkart(4), Walmart Labs(3), Splunk(3), Nvidia(3)
*/
public class FindMedianFromDataStream {
public static void main(String[] args) {
System.out.println("MedianFinderUsing2Pqs: ");
System.out.println("addNum(1), findMedian(), addNum(2), findMedian(), addNum(0), findMedian(), addNum(3)");
Mf mf = new MedianFinderUsingTwoHeaps1();
mf.addNum(1);
System.out.println(mf.findMedian());
mf.addNum(2);
System.out.println(mf.findMedian());
mf.addNum(0);
System.out.println(mf.findMedian());
mf.addNum(3);
System.out.println(mf.findMedian());
}
public static interface Mf {
public abstract void addNum(int num);
public abstract double findMedian();
}
/**
* @TimeComplexity O(logn)
* @SpaceComplexity O(n)
*/
public static class MedianFinderUsingTwoHeaps1 implements Mf {
PriorityQueue<Integer> left = new PriorityQueue<>(Comparator.reverseOrder()); // maxHeap
PriorityQueue<Integer> right = new PriorityQueue<>(); // minHeap
public void addNum(int num) {
// STEP 1: Balance by value (ordering)
if (left.isEmpty() || num <= left.peek()) left.offer(num);
else right.offer(num);
// STEP 2: Balance by size
if (left.size() > right.size() + 1) {
right.offer(left.poll());
} else if (right.size() > left.size()) { // always maintain left as biggest size
left.offer(right.poll());
}
}
public double findMedian() {
boolean isEven = (left.size() + right.size())%2 == 0;
if (isEven) {
return (left.peek() + right.peek()) / 2.0;
} else {
return left.peek();
}
}
}
/**
* @TimeComplexity O(logn)
* @SpaceComplexity O(n)
*/
public static class MedianFinderUsingTwoHeaps2 implements Mf {
PriorityQueue<Integer> left = new PriorityQueue<>(Comparator.reverseOrder()); // maxHeap
PriorityQueue<Integer> right = new PriorityQueue<>(); // minHeap
public void addNum(int num) {
left.add(num);
right.add(left.poll());
if(right.size() > left.size()){
left.add(right.poll());
}
}
public double findMedian() {
if(right.size() == left.size())
return (double) (left.peek()+right.peek())*0.5 ;
else
return (double) left.peek();
}
}
/**
* @TimeComplexity O(logn)
* @SpaceComplexity O(n)
*/
public static class MedianFinderUsingTwoHeaps3 implements Mf {
private PriorityQueue<Integer> small = new PriorityQueue<>(Collections.reverseOrder());
private PriorityQueue<Integer> large = new PriorityQueue<>();
private boolean even = true;
public void addNum(int num) {
if (even) {
large.offer(num);
small.offer(large.poll());
} else {
small.offer(num);
large.offer(small.poll());
}
even = !even;
}
public double findMedian() {
if (even)
return (small.peek() + large.peek()) / 2.0;
else
return small.peek();
}
}
/**
* @TimeComplexity O(logn)
* @SpaceComplexity O(n)
*/
public static class MedianFinderUsingTwoHeaps4 implements Mf {
private PriorityQueue<Integer> left = new PriorityQueue<>(Collections.reverseOrder()); // maxHeap
private PriorityQueue<Integer> right = new PriorityQueue<>(); // minHeap
public void addNum(int num) {
if (left.size() == right.size()) { // push to right after balancing
left.offer(num);
right.offer(left.poll());
} else { // push to left after balancing
right.offer(num);
left.offer(right.poll());
}
}
public double findMedian() {
if (left.size() == right.size()) {
return (left.peek() + right.peek()) / 2.0;
} else {
return right.peek(); // right holds extra element
}
}
}
/**
* @TimeComplexity O(logn)
* @SpaceComplexity O(n)
*/
public static class MedianFinderUsingTwoHeaps5 implements Mf {
private PriorityQueue<Integer> small = new PriorityQueue<>(Collections.reverseOrder());; // maxHeap
private PriorityQueue<Integer> large = new PriorityQueue<>(); // minHeap
public void addNum(int num) {
small.offer(num); // Step 1: add to small (maxHeap)
if (!small.isEmpty() && !large.isEmpty() && small.peek() > large.peek()) { // Step 2: ensure ordering: max(small) <= min(large)
large.offer(small.poll());
}
if (small.size() > large.size() + 1) { // Step 3: balance sizes
large.offer(small.poll());
}
if (large.size() > small.size() + 1) {
small.offer(large.poll());
}
}
public double findMedian() {
if (small.size() > large.size()) {
return small.peek();
}
if (large.size() > small.size()) {
return large.peek();
}
return (small.peek() + large.peek()) / 2.0;
}
}
/**
* @TimeComplexity O(nlogn)
* @SpaceComplexity O(n)
*/
public static class MedianFinderUsingListSort implements Mf {
List<Integer> list = new ArrayList<>();
public void addNum(int num) {
list.add(num);
}
public double findMedian() {
Collections.sort(list);
int n = list.size();
if (n % 2 == 0) return (list.get(n/2-1) + list.get(n/2))/2.00;
else return list.get(n/2);
}
}
/**
* @TimeComplexity O(nlogn)
* @SpaceComplexity O(n)
*/
public static class MedianFinderUsingHeapAndList implements Mf {
PriorityQueue<Integer> heap = new PriorityQueue<>();
public void addNum(int num) {
heap.add(num);
}
public double findMedian() { // same as sorting ---> cause new ArrayList<>(righteap); does not return the nums in sorted order
List<Integer> list = new ArrayList<>();
while (!heap.isEmpty()) list.add(heap.poll());
heap = new PriorityQueue<>(list);
int n = list.size();
if (n % 2 == 0) return (list.get(n/2-1) + list.get(n/2))/2.00;
else return list.get(n/2);
}
}
}