C++11 solution


  • 0
    S
    vector<string> findRelativeRanks(vector<int>& nums) {
        vector<int> indices(nums.size());
        iota(indices.begin(), indices.end(), 0);
        auto cmpFunc = [&](int a, int b)
        {
            return nums[a] > nums[b];  
        };
        sort(indices.begin(), indices.end(), cmpFunc);
        
        vector<string> res(nums.size());
        for (int i = 0; i < nums.size(); i++)
        {
            if (i == 0)
                res[indices[i]] = "Gold Medal";
            else if (i == 1)
                res[indices[i]] = "Silver Medal";
            else if (i == 2)
                res[indices[i]] = "Bronze Medal";
            else
                res[indices[i]] = 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.