C++ Sorting, 9ms Beats 90%, O(N*Log(N)) Time O(N) Space


  • 0
    M
    class Solution {
    public:
        vector<string> findRelativeRanks(vector<int>& nums) {
            vector<pair<int, int>> v(nums.size());
            for (int i = 0; i < nums.size(); ++i)  v[i] = {-nums[i], i};
            sort(v.begin(), v.end());
            vector<string> result(nums.size());
            for (int i = 0; i < v.size(); ++i)
                result[v[i].second] = i >= 3 ? to_string(i + 1) : i == 0 ? "Gold Medal" : i == 1 ? "Silver Medal" : i == 2 ? "Bronze Medal" : "";
            return result;
        }
    };
    

Log in to reply
 

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