patternjavaModerate
Calculate the running median
Viewed 0 times
medianrunningthecalculate
Problem
I'm trying to solve a challenge where you need to calculate the median every time you add a number.
say you have a list of numbers:
When you scan in the first number (1), you calculate the median of that.
When you scan in the second number (2), you calculate the median of 1 and 2, and so on.
My code is working, but is having timeout issues on the larger test cases:
Any tips as to how this can be optimized would be much appreciated!
say you have a list of numbers:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10When you scan in the first number (1), you calculate the median of that.
When you scan in the second number (2), you calculate the median of 1 and 2, and so on.
My code is working, but is having timeout issues on the larger test cases:
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
PriorityQueue pq = new PriorityQueue<>();
while(n-- > 0) {
pq.add(s.nextInt());
System.out.println(calculateMedian(pq));
}
}
public static float calculateMedian(PriorityQueue pq) {
PriorityQueue temp = new PriorityQueue<>(pq);
int index = 0;
float number = 0.0f;
int halfQueueSize = (temp.size() >> 1);
if(temp.size() % 2 != 0) {
while(index++ <= halfQueueSize) {
number = temp.poll();
}
return number;
} else {
while(index++ < halfQueueSize) {
number = temp.poll();
}
return ((number+temp.poll())/2.0f);
}
}
}Any tips as to how this can be optimized would be much appreciated!
Solution
The
After the
You're just misusing the queue as a sorting vehicle. That's not optimal as
It would be probably faster to use an
Inserting the element in the proper position would lead to a total complexity of \$O(n^2)\$.
A much faster solution (I hope \$O(n \log n)\$) could be implemented using a
Review
Don't use
Do you optimize division by 2 as shift or don't you? Note that it hardly matters as this is surely not the most time-consuming operation here.
You really shouldn't have two nearly identical branches. It makes it hard to spot the difference. With something like
you can use
PriorityQueue was no good idea. What are you doing?After the
nth element, you copy all the previous n-1 elements into a temporary queue, which has complexity \$O(n)\$. Then you drain half the queue, which has complexity \$O(n \log n)\$. Summed up you get \$O(n^2 \log n)\$.You're just misusing the queue as a sorting vehicle. That's not optimal as
Arrays.sort has a faster algorithm (quicksort is faster, except when it goes wrong and this one tries pretty hard to avoid going wrong).It would be probably faster to use an
int[], track the size manually and re-sort the array after each element added. This has the same complexity, but could be faster as you at least avoid the copying.Inserting the element in the proper position would lead to a total complexity of \$O(n^2)\$.
A much faster solution (I hope \$O(n \log n)\$) could be implemented using a
TreeSet with higher and lower. There are also smart algorithms allowing to compute median without sorting the collection as you sort of need to sort only the middle part.Review
public static float calculateMedian(PriorityQueue pq) {Don't use
float unless you really have to conserve memory. They're pretty imprecise and they're no faster than double, at least on modern Desktop CPUs.int halfQueueSize = (temp.size() >> 1);
if(temp.size() % 2 != 0) {Do you optimize division by 2 as shift or don't you? Note that it hardly matters as this is surely not the most time-consuming operation here.
if(temp.size() % 2 != 0) {
while(index++ <= halfQueueSize) {
number = temp.poll();
}
return number;
} else {
while(index++ < halfQueueSize) {
number = temp.poll();
}
return ((number+temp.poll())/2.0f);
}You really shouldn't have two nearly identical branches. It makes it hard to spot the difference. With something like
int halfQueueSize = (temp.size() >> 1) + (temp.size() & 1);you can use
< in both branches and pull the loop out.Code Snippets
public static float calculateMedian(PriorityQueue<Integer> pq) {int halfQueueSize = (temp.size() >> 1);
if(temp.size() % 2 != 0) {if(temp.size() % 2 != 0) {
while(index++ <= halfQueueSize) {
number = temp.poll();
}
return number;
} else {
while(index++ < halfQueueSize) {
number = temp.poll();
}
return ((number+temp.poll())/2.0f);
}int halfQueueSize = (temp.size() >> 1) + (temp.size() & 1);Context
StackExchange Code Review Q#92252, answer score: 11
Revisions (0)
No revisions yet.