> For the complete documentation index, see [llms.txt](https://dfine.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dfine.gitbook.io/leetcode/506.relative_ranks.md).

# 506.Relative Ranks

**506.Relative Ranks**

难度:Easy

> 给出 N 名运动员的成绩，找出他们的相对名次并授予前三名对应的奖牌。前三名运动员将会被分别授予 “金牌”，“银牌” 和“ 铜牌”（"Gold Medal", "Silver Medal", "Bronze Medal"）。

(注：分数越高的选手，排名越靠前。)

示例 1:

```
输入: [5, 4, 3, 2, 1]
输出: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
解释: 前三名运动员的成绩为前三高的，因此将会分别被授予 “金牌”，“银牌”和“铜牌” ("Gold Medal", "Silver Medal" and "Bronze Medal").
余下的两名运动员，我们只需要通过他们的成绩计算将其相对名次即可。
```

提示:

N 是一个正整数并且不会超过 10000。 所有运动员的成绩都不相同。

可以通过排序得出索引，然后建立排名。

```
class Solution {
public:
    vector<string> findRelativeRanks(vector<int>& nums) {
        vector<string> res(nums.size(), "");
        vector<int> index;
        for(int i=0;i<nums.size();i++)
            index.push_back(i);
        sort(index.begin(), index.end(), [&nums](int a, int b){  return nums[a] > nums[b]; });
        if(nums.size()>=1) { res[index[0]]="Gold Medal";}
        if(nums.size()>=2) {res[index[1]]="Silver Medal";}
        if(nums.size()>=3) {res[index[2]]="Bronze Medal";}
        for(int i=3;i<nums.size();i++)
        {
         //   cout<<index[i]<<" "<<endl;
            res[index[i]]=to_string(i+1);
        }
        return res;
    }
};
```

> 执行用时 : 32 ms, 在Relative Ranks的C++提交中击败了82.09% 的用户\
> 内存消耗 : 11.2 MB, 在Relative Ranks的C++提交中击败了75.21% 的用户


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/506.relative_ranks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
