luoheng
10/25/2019 - 11:50 AM

findRelativeRanks


import (
    "sort"
    "strconv"
)

func findRelativeRanks(nums []int) []string {
    pos := map[int]int{}
    for i := 0; i < len(nums); i++ {
        pos[nums[i]] = i
    }
    sort.Ints(nums)
    top3 := [3]string{
        "Gold Medal",
        "Silver Medal",
        "Bronze Medal",
    }
    res := make([]string, len(nums))
    for i := 0; i < len(nums); i++ {
        if i >= len(nums) - 3 {
            res[pos[nums[i]]] = top3[len(nums)-1-i]
        } else {
            res[pos[nums[i]]] = strconv.Itoa(len(nums)-i)
        }
    }
    return res
}