The Indian Engineer

Problem 506 Relative Ranks

Posted on 4 mins

Priority-Queue Cpp Vector Strings Heap Custom-Comparator

Problem Statement

Link - Problem 506

Question

You are given an integer array score of size n, where score[i] is the score of the ith athlete in a competition. All the scores are guaranteed to be unique.

The athletes are placed based on their scores, where the 1st place athlete has the highest score, the 2nd place athlete has the 2nd highest score, and so on. The placement of each athlete determines their rank:

Return an array answer of size n where answer[i] is the rank of the ith athlete.

Example 1

Input: score = [5,4,3,2,1]
Output: ["Gold Medal","Silver Medal","Bronze Medal","4","5"]
Explanation: The placements are [1st, 2nd, 3rd, 4th, 5th].

Example 2

Input: score = [10,3,8,9,4]
Output: ["Gold Medal","5","Bronze Medal","Silver Medal","4"]
Explanation: The placements are [1st, 5th, 3rd, 2nd, 4th].

Constraints

Solution

class Solution {
public:
    vector<string> findRelativeRanks(vector<int>& score) {
        auto comparePair = [](const pair<int,int>& a,const pair<int,int>& b)
        {
            return a.first<b.first;
        };
        priority_queue<pair<int,int>, vector<pair<int,int>>, decltype(comparePair)> maxHeap;

        for(int i = 0;i<score.size();i++)
            maxHeap.push(make_pair(score[i],i));

        vector<string>answer(score.size());
        pair<int,int> holder;
        int place = 0;
        vector<string>places = {"Gold Medal","Silver Medal","Bronze Medal"};
        while(!maxHeap.empty() && place <3)
        {
            holder = maxHeap.top();
            maxHeap.pop();
            answer[holder.second] = places[place];
            place++;
        }
        while(!maxHeap.empty())
        {

            holder = maxHeap.top();
            maxHeap.pop();
            answer[holder.second] = to_string(++place);
        }
        return answer;


    }
};

Complexity Analysis

Explanation

1. Intuition

2. Implementation


Alternate Solution

class Solution {
public:
    vector<string> findRelativeRanks(vector<int>& score) {
        int n = score.size();
        map<int, int> mp1;
        vector<string> result(n);

        for (int i = 0; i < n; i++) {
            mp1[score[i]] = i;
        }
        sort(score.begin(), score.end(), greater<int>());
        for (int i = 0; i < n; i++) {
            if (i == 0) {
                result[mp1[score[i]]] = "Gold Medal";
            } else if (i == 1) {
                result[mp1[score[i]]] = "Silver Medal";
            } else if (i == 2) {
                result[mp1[score[i]]] = "Bronze Medal";
            } else {
                result[mp1[score[i]]] = to_string(i + 1);
            }
        }

        return result;
    }
};
Time Complexity: O(nlogn)
Space Complexity: O(n)
class Solution {
public:
    vector<string> findRelativeRanks(vector<int>& score) {

        std::ios::sync_with_stdio(false);
        cin.tie(nullptr);
        cout.tie(nullptr);
        priority_queue<pair<int,int>> maxHeap;

        for(int i = 0;i<score.size();i++)
            maxHeap.push(make_pair(score[i],i));

        vector<string>answer(score.size());
        pair<int,int> holder;
        int place = 0;
        vector<string>places = {"Gold Medal","Silver Medal","Bronze Medal"};
        while(!maxHeap.empty() && place <3)
        {
            holder = maxHeap.top();
            maxHeap.pop();
            answer[holder.second] = places[place];
            place++;
        }
        while(!maxHeap.empty())
        {

            holder = maxHeap.top();
            maxHeap.pop();
            answer[holder.second] = to_string(++place);
        }
        return answer;


    }
};

This problem demonstrtaes the use of priority queue and custom comparators to solve the problem. The alternate solutions are also provided for the same problem.