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.
/*Uses binary search for Log N complexity*/intfindClosestNumberIndex(intnum,intstart,intend,conststd::vector<int>&v){constintpivot=(end+start)/2;if(v.at(pivot)==num||start>=end){returnpivot;}if(num>v.at(pivot)){returnfindClosestNumberIndex(num,start,pivot-1,v);}if(num<v.at(pivot)){returnfindClosestNumberIndex(num,pivot+1,end,v);}return-1;}std::vector<int>climbingLeaderboard(std::vector<int>ranked,std::vector<int>player){/*Map used for fast search access*/std::map<int,int>posScores{{ranked.at(0),1}};intcl=1;for(inti=1;i<ranked.size();++i){if(ranked.at(i)!=ranked.at(i-1)){++cl;posScores.emplace(ranked.at(i),cl);}}std::vector<int>result;result.reserve(player.size());for(constintplayerScore:player){intpos=-1;/*If the exact same value is found in the map, we don't need to find the closest number*/autoIt=posScores.find(playerScore);if(It!=posScores.end()){pos=It->second;}else{constintclosestNumIndex=findClosestNumberIndex(playerScore,0,ranked.size()-1,ranked);conststd::pair<int,int>pairFound=*posScores.find(ranked.at(closestNumIndex));pairFound.first>playerScore?pos=pairFound.second+1:pos=pairFound.second;}result.push_back(pos);}returnresult;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Climbing the Leaderboard
You are viewing a single comment's thread. Return to all comments →