C++ solution using map. How to improve?


  • 0
    K
    class Solution {
    public:
        vector<string> findRelativeRanks(vector<int>& nums) {
            map<int,int,greater<int>>mp;
            for(int i=0;i<nums.size();++i)
                mp[nums[i]]=i;
            vector<string> res(int(nums.size()),"");
            string metal[3]={"Gold Medal", "Silver Medal", "Bronze Medal"};
            int idx=1;
            for(auto it=mp.begin();it!=mp.end();++it)
            {
                int rank=idx++;
                res[it->second]=rank<=3?metal[rank-1]:to_string(rank);
            }
            return res;
        }
    };

Log in to reply
 

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