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.
The idea is to create a set of ranked with distinct values
Now we start from the backward of the leadership board to find the position for the player score, that by reducing the index
Once we find the score position then insert index + 2 to the resulted array
function climbingLeaderboard(ranked, player) {
// Write your code here
const leaderboard = Array.from(new Set(ranked))
const result = []
let index = leaderboard.length - 1 // Start from the lowest rank
for (let score of player) {
// Move up the leaderboard until you find the position for the player's score
while (index >= 0 && score >= leaderboard[index]) {
index--
}
// The player's rank is index + 2 (since index + 1 would be 0-based)
result.push(index + 2)
}
return result;
}
`
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 →
function climbingLeaderboard(ranked, player) { // Write your code here const leaderboard = Array.from(new Set(ranked)) const result = [] let index = leaderboard.length - 1 // Start from the lowest rank
} `