Just using vector is enough.


  • 0
    N
    class Solution {
    public:
        vector<string> findRelativeRanks(vector<int>& nums) {
            vector<pair<int, int>> atheletes;
            for (int i = 0; i < nums.size(); ++i) { atheletes.push_back({nums[i], i}); }
            sort(atheletes.rbegin(), atheletes.rend());
            vector<string> res(nums.size());
            for (int i = 0; i < nums.size(); ++i) {
                auto p = atheletes[i];
                if (i == 0) {
                    res[p.second] = "Gold Medal";
                } else if (i == 1) {
                    res[p.second] = "Silver Medal";
                } else if (i == 2) {
                    res[p.second] = "Bronze Medal";
                } else {
                    res[p.second] = to_string(i + 1);
                }
            }
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.