You are viewing a single comment's thread. Return to all comments →
int n = a.length; double[] db = new double[n]; PriorityQueue<Integer> minh = new PriorityQueue<Integer>(); PriorityQueue<Integer> maxh = new PriorityQueue<Integer>(Collections.reverseOrder()); for(int i=0; i<n; i++){ if(maxh.isEmpty()){ maxh.add(a[i]); } else if(maxh.size() == minh.size()){ if(n<minh.peek()) maxh.add(a[i]); else{ minh.add(a[i]); maxh.add(minh.poll()); } } else if(maxh.size() > minh.size()){ if(n > maxh.peek()){ minh.add(a[i]); } else{ maxh.add(a[i]); minh.add(maxh.poll()); } } if(maxh.size() == minh.size()) db[i] = (maxh.peek() + minh.peek())/2.0; else db[i] = (maxh.peek()); } return db;
Seems like cookies are disabled on this browser, please enable them to open this website
Find the Running Median
You are viewing a single comment's thread. Return to all comments →
Here is my code, and all the test cases have failed except the first one.