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.
Solution in TypeScript. Complexity O(r), where r is the length of ranked array with O(p) space complexity.
No sorting functions
No set / dictionary
functionclimbingLeaderboard(ranked:number[],player:number[]):number[]{letplayer_i=player.length-1letresults=newArray(player.length).fill(-1);letprev_rank=nullletrank_shift=0// O(r) letrank_i=0while(rank_i<ranked.length&&player_i>=0){constcurrent_rank=ranked[rank_i]constcurrent_player=player[player_i]if(current_player===current_rank){results[player_i]=(rank_i+1-rank_shift)player_i--}elseif(current_player>current_rank){results[player_i]=(rank_i+1-rank_shift)player_i--}elseif(current_player<current_rank){rank_i++// Shift the rank if there are ranked score duplicates.if(prev_rank!==null){if(prev_rank===current_rank){rank_shift++}}// Assign to the previous rank to compare in the next iterationprev_rank=current_rank}}// Check the rest of the player scoreswhile(player_i>-1){constcurrent_player=player[player_i]if(current_player===ranked[ranked.length-1]){results[player_i]=(ranked.length-rank_shift)}elseif(current_player<ranked[ranked.length-1]){results[player_i]=(ranked.length+1-rank_shift)}player_i--}returnresults}
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 →
Solution in TypeScript. Complexity O(r), where r is the length of ranked array with O(p) space complexity.