C++ clean solution using hash table


  • 0
        vector<string> findRelativeRanks(vector<int>& nums) {
            vector<string>res;
            unordered_map<int,string>m;
            vector<int>v=nums;
            sort(v.begin(),v.end(),greater<int>());
            for(int i=0;i<v.size();i++){
                if(i==0) m[v[0]]="Gold Medal";
                else if(i==1) m[v[1]]="Silver Medal";
                else if(i==2) m[v[2]]="Bronze Medal";
                else m[v[i]]=to_string(i+1);
            }
            for(int n:nums) res.push_back(m[n]);
            return res;
        }
    

  • 0
    W

    sort(v.begin(),v.end(),[](int a,int b){return a>b;});

    here , just use :
    sort(v.begin(), v.end(), greater<int>());


  • 0

    @wangying_love_c- Thanks for sharing!


Log in to reply
 

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