Leetcode 506 Solution

This article provides solution to leetcode question 506 (relative-ranks)

https://leetcode.com/problems/relative-ranks

Solution

class Solution {
public:
    string get_rank(int rank)
    {
        if (rank == 1)
            return "Gold Medal";
        else if (rank == 2)
            return "Silver Medal";
        else if (rank == 3)
            return "Bronze Medal";
        else
            return to_string(rank);
    }

    vector<string> findRelativeRanks(vector<int>& nums) {
        vector<pair<int, int>> a;

        for (int i = 0; i < nums.size(); i++)
            a.push_back(make_pair(nums[i], i));
        sort(a.begin(), a.end());

        vector<string> res(nums.size());
        for (int i = a.size() - 1; i >= 0; i--)
        {
            int rank = a.size() - i;
            res[a[i].second] = get_rank(rank);
        }

        return res;
    }
};