mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 02:04:31 +08:00
* #4382 Bug Fix * #4382 Bug Fix * Made Requested Changes * Made Requested Changes * Made Requested Changes * Made Requested Changes * Made Requested Changes * Made Requested Changes * Made Requested Changes * Update src/main/java/com/thealgorithms/misc/MedianOfRunningArray.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update src/main/java/com/thealgorithms/misc/MedianOfRunningArray.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
@ -8,13 +8,13 @@ import java.util.PriorityQueue;
|
||||
*/
|
||||
public class MedianOfRunningArray {
|
||||
|
||||
private PriorityQueue<Integer> p1;
|
||||
private PriorityQueue<Integer> p2;
|
||||
private PriorityQueue<Integer> maxHeap;
|
||||
private PriorityQueue<Integer> minHeap;
|
||||
|
||||
// Constructor
|
||||
public MedianOfRunningArray() {
|
||||
this.p1 = new PriorityQueue<>(Collections.reverseOrder()); // Max Heap
|
||||
this.p2 = new PriorityQueue<>(); // Min Heap
|
||||
this.maxHeap = new PriorityQueue<>(Collections.reverseOrder()); // Max Heap
|
||||
this.minHeap = new PriorityQueue<>(); // Min Heap
|
||||
}
|
||||
|
||||
/*
|
||||
@ -22,32 +22,30 @@ public class MedianOfRunningArray {
|
||||
and upper half to min heap
|
||||
*/
|
||||
public void insert(Integer e) {
|
||||
p2.add(e);
|
||||
if (p2.size() - p1.size() > 1) {
|
||||
p1.add(p2.remove());
|
||||
if (!minHeap.isEmpty() && e < minHeap.peek()) {
|
||||
maxHeap.offer(e);
|
||||
if (maxHeap.size() > minHeap.size() + 1) {
|
||||
minHeap.offer(maxHeap.poll());
|
||||
}
|
||||
} else {
|
||||
minHeap.offer(e);
|
||||
if (minHeap.size() > maxHeap.size() + 1) {
|
||||
maxHeap.offer(minHeap.poll());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Returns median at any given point
|
||||
*/
|
||||
public Integer median() {
|
||||
if (p1.size() == p2.size()) {
|
||||
return (p1.peek() + p2.peek()) / 2;
|
||||
public double median() {
|
||||
if (maxHeap.isEmpty() && minHeap.isEmpty()) {
|
||||
throw new IllegalArgumentException("Enter at least 1 element, Median of empty list is not defined!");
|
||||
}
|
||||
return p1.size() > p2.size() ? p1.peek() : p2.peek();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/*
|
||||
Testing the median function
|
||||
*/
|
||||
|
||||
MedianOfRunningArray p = new MedianOfRunningArray();
|
||||
int[] arr = {10, 7, 4, 9, 2, 3, 11, 17, 14};
|
||||
for (int i = 0; i < 9; i++) {
|
||||
p.insert(arr[i]);
|
||||
System.out.print(p.median() + " ");
|
||||
if (maxHeap.size() == minHeap.size()) {
|
||||
return (maxHeap.peek() + minHeap.peek()) / 2.0;
|
||||
}
|
||||
return maxHeap.size() > minHeap.size() ? maxHeap.peek() * 1.0 : minHeap.peek() * 1.0;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user