C++ Coding Exercise – Use HashTable/Priority Queue to Compute the Relative Ranks


Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: “Gold Medal”, “Silver Medal” and “Bronze Medal”.

Example 1:
Input: [5, 4, 3, 2, 1]
Output: [“Gold Medal”, “Silver Medal”, “Bronze Medal”, “4”, “5”]
Explanation: The first three athletes got the top three highest scores, so they got “Gold Medal”, “Silver Medal” and “Bronze Medal”.
For the left two athletes, you just need to output their relative ranks according to their scores.
Note:
N is a positive integer and won’t exceed 10,000.
All the scores of athletes are guaranteed to be unique.

The idea is to compute the ranks for each score. In order to do this, you can sort the array in the descending order. We can use a hash table (unordered_map) to store the ranks from the biggest scores to the smallest scores. However, you still need to preserve the original array to print out the relative ranks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
    vector<string> findRelativeRanks(vector<int>& nums) {
        unordered_map<int, int> x;
        vector<int> n(nums);
        std::sort(n.begin(), n.end(), [](const int a, const int b) {return a > b; });
        int r = 1;
        for (int i = 0; i < n.size(); ++ i) {
            x[n[i]] = r ++;
        }
        vector<string> res;
        for (int i = 0; i < nums.size(); ++ i) {
            switch (x[nums[i]]) {
                case 1: res.push_back("Gold Medal"); break;
                case 2: res.push_back("Silver Medal"); break;
                case 3: res.push_back("Bronze Medal"); break;
                default: res.push_back(std::to_string(x[nums[i]])); break;
            }
        }
        return res;
    }
};
class Solution {
public:
    vector<string> findRelativeRanks(vector<int>& nums) {
        unordered_map<int, int> x;
        vector<int> n(nums);
        std::sort(n.begin(), n.end(), [](const int a, const int b) {return a > b; });
        int r = 1;
        for (int i = 0; i < n.size(); ++ i) {
            x[n[i]] = r ++;
        }
        vector<string> res;
        for (int i = 0; i < nums.size(); ++ i) {
            switch (x[nums[i]]) {
                case 1: res.push_back("Gold Medal"); break;
                case 2: res.push_back("Silver Medal"); break;
                case 3: res.push_back("Bronze Medal"); break;
                default: res.push_back(std::to_string(x[nums[i]])); break;
            }
        }
        return res;
    }
};

You can also use the std::priority_queue to compute the ranks. The priority queue pops always the largest element (in a heap-like structure). The C++ source code to solve this problem using the priority_queue is quite similar (push each element into the queue and pop out the largest element until the queue is empty)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
    vector<string> findRelativeRanks(vector<int>& nums) {
        priority_queue<int> pq;
        for (int n: nums) {
            pq.push(n);            
        }
        unordered_map<int, int> ranks;
        int r = 1;
        while (!pq.empty()) {
            int n = pq.top();
            pq.pop();
            ranks[n] = r ++;
        }        
        vector<string> res;
        for (int i = 0; i < nums.size(); ++ i) {
            switch (ranks[nums[i]]) {
                case 1: res.push_back("Gold Medal"); break;
                case 2: res.push_back("Silver Medal"); break;
                case 3: res.push_back("Bronze Medal"); break;
                default: res.push_back(std::to_string(ranks[nums[i]])); break;
            }
        }
        return res;
    }
};
class Solution {
public:
    vector<string> findRelativeRanks(vector<int>& nums) {
        priority_queue<int> pq;
        for (int n: nums) {
            pq.push(n);            
        }
        unordered_map<int, int> ranks;
        int r = 1;
        while (!pq.empty()) {
            int n = pq.top();
            pq.pop();
            ranks[n] = r ++;
        }        
        vector<string> res;
        for (int i = 0; i < nums.size(); ++ i) {
            switch (ranks[nums[i]]) {
                case 1: res.push_back("Gold Medal"); break;
                case 2: res.push_back("Silver Medal"); break;
                case 3: res.push_back("Bronze Medal"); break;
                default: res.push_back(std::to_string(ranks[nums[i]])); break;
            }
        }
        return res;
    }
};

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
a WordPress rating system
499 words
Last Post: How to Convert/Transfer Steem or Steem Dollars (SBD) to Bitcoins?
Next Post: Efficient Hamming Distance Algorithms Between Two Integers in C++

The Permanent URL is: C++ Coding Exercise – Use HashTable/Priority Queue to Compute the Relative Ranks

Leave a Reply