Skip to content

506. Relative Ranks #156

Answered by topugit
mah-shamim asked this question in Q&A
Jul 30, 2024 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

We can follow these steps:

  1. Sort the Scores: We need to know the relative order of each score, so we sort the scores in descending order.
  2. Assign Ranks: Once we have the sorted scores, we can map the ranks to the original scores based on their positions.
  3. Map the Ranks Back to the Original Array: Using the original indices of the scores, assign the appropriate ranks.

Let's implement this solution in PHP: 506. Relative Ranks

<?php
function findRelativeRanks($score) {
    $n = count($score);
    $ranked = $score;
    
    // Step 1: Sort the scores in descending order while keeping track of original indices
    arsort($ranked);
    
    // Step 2: Assign ranks
    $ranks = array();
    $index

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mah-shamim
Comment options

mah-shamim Sep 3, 2024
Maintainer Author

@topugit
Comment options

topugit Sep 3, 2024
Collaborator

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants