final solution with tests

This commit is contained in:
priyank-sriv 2019-12-02 19:40:38 +05:30
parent 2ec45fe5b8
commit 76fa560184
3 changed files with 91 additions and 7 deletions

View File

@ -0,0 +1,42 @@
package com.baeldung.algorithms.integerstreammedian;
import java.util.PriorityQueue;
import java.util.Queue;
import static java.util.Comparator.reverseOrder;
public class MedianOfIntegerStream {
private Queue<Integer> minHeap, maxHeap;
MedianOfIntegerStream() {
minHeap = new PriorityQueue<>();
maxHeap = new PriorityQueue<>(reverseOrder());
}
void add(int num) {
if (!minHeap.isEmpty() && num < minHeap.peek()) {
maxHeap.offer(num);
if (maxHeap.size() > minHeap.size() + 1) {
minHeap.offer(maxHeap.poll());
}
} else {
minHeap.offer(num);
if (minHeap.size() > maxHeap.size() + 1) {
maxHeap.offer(minHeap.poll());
}
}
}
double getMedian() {
int median;
if (minHeap.size() < maxHeap.size()) {
median = maxHeap.peek();
} else if (minHeap.size() > maxHeap.size()) {
median = minHeap.peek();
} else {
median = (minHeap.peek() + maxHeap.peek()) / 2;
}
return median;
}
}

View File

@ -1,25 +1,26 @@
package com.baeldung.algorithms.datastreammedian; package com.baeldung.algorithms.integerstreammedian;
import java.util.PriorityQueue; import java.util.PriorityQueue;
import java.util.Queue; import java.util.Queue;
import static java.util.Comparator.reverseOrder; import static java.util.Comparator.reverseOrder;
public class DataStreamMedian { public class MedianOfIntegerStream2 {
private Queue<Integer> minHeap, maxHeap; private Queue<Integer> minHeap, maxHeap;
DataStreamMedian() { MedianOfIntegerStream2() {
minHeap = new PriorityQueue<>(); minHeap = new PriorityQueue<>();
maxHeap = new PriorityQueue<>(reverseOrder()); maxHeap = new PriorityQueue<>(reverseOrder());
} }
void add(int num) { void add(int num) {
minHeap.offer(num); if (minHeap.size() == maxHeap.size()) {
maxHeap.offer(minHeap.poll()); maxHeap.offer(num);
if (minHeap.size() < maxHeap.size()) {
minHeap.offer(maxHeap.poll()); minHeap.offer(maxHeap.poll());
} else {
minHeap.offer(num);
maxHeap.offer(minHeap.poll());
} }
} }

View File

@ -0,0 +1,41 @@
package com.baeldung.algorithms.integerstreammedian;
import org.junit.Test;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
public class MedianOfIntegerStreamTest {
@Test
public void givenStreamOfIntegers_whenAnElementIsRead_thenMedianChangesWithApproach1() {
MedianOfIntegerStream mis = new MedianOfIntegerStream();
for (Map.Entry<Integer, Double> e : testcaseFixture().entrySet()) {
mis.add(e.getKey());
assertEquals(e.getValue(), (Double) mis.getMedian());
}
}
@Test
public void givenStreamOfIntegers_whenAnElementIsRead_thenMedianChangesWithApproach2() {
MedianOfIntegerStream2 mis = new MedianOfIntegerStream2();
for (Map.Entry<Integer, Double> e : testcaseFixture().entrySet()) {
mis.add(e.getKey());
assertEquals(e.getValue(), (Double) mis.getMedian());
}
}
private Map<Integer, Double> testcaseFixture() {
return new LinkedHashMap<Integer, Double>() {{
put(1, 1d);
put(7, 4d);
put(5, 5d);
put(8, 6d);
put(3, 5d);
put(9, 6d);
put(4, 5d);
}};
}
}