From 76fa560184fccbe95e28595f641f1bcc8386de83 Mon Sep 17 00:00:00 2001 From: priyank-sriv Date: Mon, 2 Dec 2019 19:40:38 +0530 Subject: [PATCH] final solution with tests --- .../MedianOfIntegerStream.java | 42 +++++++++++++++++++ .../MedianOfIntegerStream2.java} | 15 +++---- .../MedianOfIntegerStreamTest.java | 41 ++++++++++++++++++ 3 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStream.java rename algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/{datastreammedian/DataStreamMedian.java => integerstreammedian/MedianOfIntegerStream2.java} (66%) create mode 100644 algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStreamTest.java diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStream.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStream.java new file mode 100644 index 0000000000..9d3b699956 --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStream.java @@ -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 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; + } +} diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/datastreammedian/DataStreamMedian.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStream2.java similarity index 66% rename from algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/datastreammedian/DataStreamMedian.java rename to algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStream2.java index 64f78227d5..8aa7e6f6af 100644 --- a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/datastreammedian/DataStreamMedian.java +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStream2.java @@ -1,25 +1,26 @@ -package com.baeldung.algorithms.datastreammedian; +package com.baeldung.algorithms.integerstreammedian; import java.util.PriorityQueue; import java.util.Queue; import static java.util.Comparator.reverseOrder; -public class DataStreamMedian { +public class MedianOfIntegerStream2 { private Queue minHeap, maxHeap; - DataStreamMedian() { + MedianOfIntegerStream2() { minHeap = new PriorityQueue<>(); maxHeap = new PriorityQueue<>(reverseOrder()); } void add(int num) { - minHeap.offer(num); - maxHeap.offer(minHeap.poll()); - - if (minHeap.size() < maxHeap.size()) { + if (minHeap.size() == maxHeap.size()) { + maxHeap.offer(num); minHeap.offer(maxHeap.poll()); + } else { + minHeap.offer(num); + maxHeap.offer(minHeap.poll()); } } diff --git a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStreamTest.java b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStreamTest.java new file mode 100644 index 0000000000..8c4e0f89b1 --- /dev/null +++ b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/integerstreammedian/MedianOfIntegerStreamTest.java @@ -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 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 e : testcaseFixture().entrySet()) { + mis.add(e.getKey()); + assertEquals(e.getValue(), (Double) mis.getMedian()); + } + } + + private Map testcaseFixture() { + return new LinkedHashMap() {{ + put(1, 1d); + put(7, 4d); + put(5, 5d); + put(8, 6d); + put(3, 5d); + put(9, 6d); + put(4, 5d); + }}; + } +}