c++ solution


  • 0
    W
    class Solution {
    public:
    	vector<string> findRelativeRanks(vector<int>& nums) {
    		map<int, int, greater<int>> idxs;
    		for (int i = 0; i < nums.size(); ++i) {
    			idxs[nums[i]] = i;
    		}
    		vector<string> res(nums.size(), "");
    		int i = 0;
    		for (auto it = idxs.begin(); it != idxs.end(); ++it, ++i) {
    			string v;
    			if (i == 0) v = "Gold Medal";
    			else if (i == 1) v = "Silver Medal";
    			else if (i == 2) v = "Bronze Medal";
    			else {
    				int rank = i + 1;
    				stringstream ss;
    				ss << rank;
    				v = ss.str();
    			}
    			res[it->second] = v;
    		}
    		return res;
    	}
    };
    

Log in to reply
 

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