Source : https://www.hackerrank.com/challenges/ctci-find-the-running-median
The median of a dataset of integers is the midpoint value of the dataset for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your dataset of integers in non-decreasing order, then:
- If your dataset contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted dataset , is the median.
- If your dataset contains an even number of elements, the median is the average of the two middle elements of the sorted sample. In the sorted dataset , is the median.
Given an input stream of integers, you must perform the following task for each integer:
- Add the integer to a running list of integers.
- Find the median of the updated list (i.e., for the first element through the element).
- Print the list's updated median on a new line. The printed value must be a double-precision number scaled to decimal place (i.e., format).
Input Format
The first line contains a single integer, , denoting the number of integers in the data stream.
Each line of the subsequent lines contains an integer, , to be added to your list.
Constraints
Output Format
After each new integer is added to the list, print the list's updated median on a new line as a single double-precision number scaled to decimal place (i.e., format).
Sample Input
61245387
Sample Output
12.08.05.04.55.06.0
Explanation
There are integers, so we must print the new median on a new line as each integer is added to the list:
Source : https://www.hackerrank.com/challenges/ctci-find-the-running-median
Solution
// Karthikalapati.blogspot.com | |
import java.util.Scanner; | |
import java.util.PriorityQueue; | |
import java.util.Collections; | |
// - We use 2 Heaps to keep track of median | |
// - We make sure that 1 of the following conditions is always true: | |
// 1) maxHeap.size() == minHeap.size() | |
// 2) maxHeap.size() - 1 = minHeap.size() | |
public class Solution { | |
private static PriorityQueue<Integer> maxHeap = new PriorityQueue(Collections.reverseOrder()); // keeps track of the SMALL numbers | |
private static PriorityQueue<Integer> minHeap = new PriorityQueue(); // keeps track of the LARGE numbers | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
int n = scan.nextInt(); | |
int [] array = new int[n]; | |
for (int i = 0; i < n; i++) { | |
array[i] = scan.nextInt(); | |
} | |
scan.close(); | |
medianTracker(array); | |
} | |
public static void medianTracker(int [] array) { | |
for (int i = 0; i < array.length; i++) { | |
addNumber(array[i]); | |
System.out.println(getMedian()); | |
} | |
} | |
private static void addNumber(int n) { | |
if (maxHeap.isEmpty()) { | |
maxHeap.add(n); | |
} else if (maxHeap.size() == minHeap.size()) { | |
if (n < minHeap.peek()) { | |
maxHeap.add(n); | |
} else { | |
minHeap.add(n); | |
maxHeap.add(minHeap.remove()); | |
} | |
} else if (maxHeap.size() > minHeap.size()) { | |
if (n > maxHeap.peek()) { | |
minHeap.add(n); | |
} else { | |
maxHeap.add(n); | |
minHeap.add(maxHeap.remove()); | |
} | |
} | |
// maxHeap will never have fewer elements than minHeap | |
} | |
private static double getMedian() { | |
if (maxHeap.isEmpty()) { | |
return 0; | |
} else if (maxHeap.size() == minHeap.size()) { | |
return (maxHeap.peek() + minHeap.peek()) / 2.0; | |
} else { // maxHeap must have more elements than minHeap | |
return maxHeap.peek(); | |
} | |
} | |
} |
No comments:
Post a Comment