We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Using a multiset and moving iterator to median left/right.
`voidrunningMedian(vector<int>arr){// Print your answer within the functionstd::cout<<std::fixed;std::cout<<std::setprecision(1);std::multiset<int>ms;floatmedian=0.0;cout<<float(arr[0])<<endl;ms.insert(arr[0]);std::multiset<int>::iteratormed=ms.begin();for(size_tcs=1;cs<arr.size();cs++){ms.insert(arr[cs]);// odd,if(cs%2==0){if(arr[cs]>=*med)med=next(med,1);median=*med;}//evenelse{if(arr[cs]<*med)med=next(med,-1);median=(*med+*(std::next(med,1)))/2.0;}cout<<float(median)<<endl;}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Heaps: Find the Running Median
You are viewing a single comment's thread. Return to all comments →
Using a multiset and moving iterator to median left/right.