C++ 7 line solution easy to understand


  • 0
    • list item
    class SolutionSimply {
    public:
        vector<string> findRelativeRanks(vector<int>& nums) {
                vector<string> result;
                vector<int> numscopy(nums);
                string strs[]={"Gold Medal","Silver Medal","Bronze Medal"};
                sort(numscopy.begin(),numscopy.end());
                for(int i=0;i<nums.size();i++)result.push_back(to_string(nums.size()-distance(numscopy.begin(),find(numscopy.begin(),numscopy.end(),nums[i]))));           
                for(int i=0;i<min((int)nums.size(),3);i++)result[distance(nums.begin(),find(nums.begin(),nums.end(),numscopy[nums.size()-i-1]))]=strs[i];
                return result;
        }
    };
    

Log in to reply
 

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