You are given an integer array score of size n, where score[i] is the score of the ith athlete in a competition. All the scores are guaranteed to be unique.

The athletes are placed based on their scores, where the 1st place athlete has the highest score, the 2nd place athlete has the 2nd highest score, and so on. The placement of each athlete determines their rank:

  • The 1st place athlete's rank is "Gold Medal ".
  • The 2nd place athlete's rank is "Silver Medal ".
  • The 3rd place athlete's rank is "Bronze Medal ".
  • For the 4th place to the nth place athlete, their rank is their placement number (i.e., the xth place athlete's rank is "x ").

Return an array answer of size n where answer[i] is the rank of the ith athlete.

Example 1:

Input: score = [5,4,3,2,1] Output: [ "Gold Medal ", "Silver Medal ", "Bronze Medal ", "4 ", "5 "] Explanation: The placements are [1st, 2nd, 3rd, 4th, 5th].

Example 2:

Input: score = [10,3,8,9,4] Output: [ "Gold Medal ", "5 ", "Bronze Medal ", "Silver Medal ", "4 "] Explanation: The placements are [1st, 5th, 3rd, 2nd, 4th].

Constraints:

  • n == score.length
  • 1 <= n <= 104
  • 0 <= score[i] <= 106
  • All the values in score are unique.

Note: This problem is from LeetCode.
βœ… Compiled
βœ… Executed
βœ… Correct
program main
    implicit none

    integer, parameter :: n = 5
    integer :: score(n) = [5, 4, 3, 2, 1]
    character(len=10) :: answer(n)

    call solve(score, answer)

    write (*, '(A)') answer

contains

    subroutine solve(score, answer)
        implicit none
        integer, intent(in) :: score(:)
        character(len=10), intent(out) :: answer(:)

        integer :: i, j

        do i = 1, size(score)
            select case(i)
                case(1)
                    answer(i) = "Gold Medal "
                case(2)
                    answer(i) = "Silver Medal "
                case(3)
                    answer(i) = "Bronze Medal "
                case default
                    write (answer(i), '(I0)') i
            end select
        end do
    end subroutine solve
end program main
❌ Compiled
❌ Executed
❌ Correct
module Fortran
contains
    function get_rank(score) result(answer)
        implicit none
        integer, dimension(:), intent(in) :: score
        character(len=10), dimension(size(score)) :: answer

        ! Declare variables
        integer :: i, n
        integer, dimension(size(score)) :: sorted_score

        ! Sort the scores in descending order
        sorted_score = score
        call sort(sorted_score)

        ! Assign ranks based on the sorted scores
        do i = 1, size(score)
            if (i <= 3) then
                answer(i) = trim(adjustl(str(i))) // " Medal "
            else
                answer(i) = trim(adjustl(str(i))) // " "
            end if
        end do

        ! Print the results
        do i = 1, size(score)
            write (*,*) answer(i), sorted_score(i)
        end do

    end function get_rank
end module Fortran

program test_get_rank
    use Fortran
    implicit none
    integer, parameter :: n = 5
    integer, dimension(n) :: score = [5, 4, 3, 2, 1]
    character(len=10), dimension(n) :: answer

    ! Test case 1
    answer = get_rank(score)
    write (*,*) "Test case 1:"
    write (*,*) "Expected: " // "Gold Medal " // "Silver Medal " // "Bronze Medal " // "4 " // "5 "
    write (*,*) "Actual: " // answer

    ! Test case 2
    score = [10, 3, 8, 9, 4]
    answer = get_rank(score)
    write (*,*) "Test case 2:"
    write (*,*) "Expected: " // "Gold Medal " // "5 " // "Bronze Medal " // "Silver Medal " // "4 "
    write (*,*) "Actual: " // answer
end program test_get_rank
🌐 Data from online sources
def find_relative_ranks(score):
    rank_sort = sorted(range(len(score)), key=lambda i: score[i], reverse=True)
    result = [0] * len(score)
    for i, rank in enumerate(rank_sort):
        result[rank] = i + 1
    return result
The algorithm works in the following steps:
  1. Create an array of indices ranging from 0 to n-1, where n is the size of the given score array.

  2. Sort the indices array based on their corresponding score values in descending order. So, after sorting, the first element will be the index of the highest score in the original score array.

  3. Create a result array of size n.

  4. Iterate over the sorted indices array and assign its rank (1-based) to the corresponding index in the result array.

  5. Return the result array that now contains the ranks of athletes.

🌐 Data from online sources
#include <vector>
#include <algorithm>

std::vector<int> findRelativeRanks(std::vector<int>& score) {
    std::vector<int> rank_sort(score.size());
    for(int i = 0; i < score.size(); ++i) rank_sort[i] = i;
    std::sort(rank_sort.begin(), rank_sort.end(), [&](int i1, int i2) { return score[i1] > score[i2]; });
    std::vector<int>  result(score.size());
    for(int i = 0; i < rank_sort.size(); ++i) result[rank_sort[i]] = i + 1;
    return result;
}
The algorithm works in the following steps:
  1. Create an array of indices ranging from 0 to n-1, where n is the size of the given score array.

  2. Sort the indices array based on their corresponding score values in descending order. So, after sorting, the first element will be the index of the highest score in the original score array.

  3. Create a result array of size n.

  4. Iterate over the sorted indices array and assign its rank (1-based) to the corresponding index in the result array.

  5. Return the result array that now contains the ranks of athletes.