simple C++ sort&unordered_map


  • 0
    B
        vector<string> findRelativeRanks(vector<int>& nums) {
            vector<string> res;
            vector<int> copy = nums;
            sort(copy.begin(), copy.end(), greater<int>());
            unordered_map<int,int> um;
            for(int i=0;i<copy.size();i++) {
                um[copy[i]] = i;
            }
            const vector<string> medals = {"Gold Medal", "Silver Medal", "Bronze Medal"};
            for(int num:nums) {
                int i=um[num];
                res.push_back(i<3?medals[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.