You are viewing a single comment's thread. Return to all comments →
vector<double> runningMedian(vector<int> arr) { priority_queue<int,vector<int>,greater<int>>minHeap; priority_queue<int> maxHeap; vector<double> ans; minHeap.push(arr[0]); ans.push_back((double)arr[0]); for(int i=1;i<arr.size();i++){ if(minHeap.top()<arr[i]){ minHeap.push(arr[i]); printf("%d %d %d\n",minHeap.top(),minHeap.size(),maxHeap.size()); } else{ maxHeap.push(arr[i]); } if((minHeap.size() -maxHeap.size())>1){ maxHeap.push(minHeap.top()); minHeap.pop(); printf(" %d %d\n",minHeap.top(),maxHeap.top()); } if((maxHeap.size()-minHeap.size())>1){ printf("-6 %d\n",maxHeap.size()-minHeap.size()); minHeap.push(maxHeap.top()); maxHeap.pop(); } if(i%2==1){ int aa=minHeap.top()+maxHeap.top(); ans.push_back((double)aa/2.0); } else{ if(minHeap.size()>maxHeap.size()){ printf("%d -1\n",minHeap.top()); ans.push_back((double)minHeap.top()); } else{ ans.push_back((double)maxHeap.top()); } } } return ans; }
Any Idea why does this function NOT work?
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 →
Any Idea why does this function NOT work?