Relative Ranks – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
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:
- The
1st
place athlete’s rank is"Gold Medal"
. - The
2nd
place athlete’s rank is"Silver Medal"
. - The
3rd
place athlete’s rank is"Bronze Medal"
. - For the
4th
place to thenth
place athlete, their rank is their placement number (i.e., thexth
place athlete’s rank is"x"
).
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:
n == score.length
1 <= n <= 104
0 <= score[i] <= 106
- All the values in
score
are unique.
C++ Relative Ranks LeetCode Solution
class Solution {
public:
vector<string> findRelativeRanks(vector<int>& nums) {
priority_queue<pair<int,int> > pq;
for(int i=0;i<nums.size();i++)
{
pq.push(make_pair(nums[i],i));
}
vector<string> res(nums.size(),"");
int count = 1;
for(int i=0; i<nums.size();i++)
{
if(count==1) {res[pq.top().second] = "Gold Medal"; count++;}
else if(count==2) {res[pq.top().second] = "Silver Medal"; count++;}
else if(count==3) {res[pq.top().second] = "Bronze Medal"; count++;}
else {res[pq.top().second] = to_string(count); count++;}
pq.pop();
}
return res;
}
};
Java Relative Ranks LeetCode Solution
public class Solution {
public String[] findRelativeRanks(int[] nums) {
Integer[] index = new Integer[nums.length];
for (int i = 0; i < nums.length; i++) {
index[i] = i;
}
Arrays.sort(index, (a, b) -> (nums[b] - nums[a]));
String[] result = new String[nums.length];
for (int i = 0; i < nums.length; i++) {
if (i == 0) {
result[index[i]] = "Gold Medal";
}
else if (i == 1) {
result[index[i]] = "Silver Medal";
}
else if (i == 2) {
result[index[i]] = "Bronze Medal";
}
else {
result[index[i]] = (i + 1) + "";
}
}
return result;
}
}
Python 3 Relative Ranks LeetCode Solution
def findRelativeRanks(self, nums):
sort = sorted(nums)[::-1]
rank = ["Gold Medal", "Silver Medal", "Bronze Medal"] + map(str, range(4, len(nums) + 1))
return map(dict(zip(sort, rank)).get, nums)
Array-1180
String-562
Hash Table-412
Dynamic Programming-390
Math-368
Sorting-264
Greedy-257
Depth-First Search-256
Database-215
Breadth-First Search-200
Tree-195
Binary Search-191
Matrix-176
Binary Tree-160
Two Pointers-151
Bit Manipulation-140
Stack-133
Heap (Priority Queue)-117
Design-116
Graph-108
Simulation-103
Prefix Sum-96
Backtracking-92
Counting-86
Sliding Window-73
Linked List-69
Union Find-66
Ordered Set-48
Monotonic Stack-47
Recursion-43
Trie-41
Binary Search Tree-40
Divide and Conquer-40
Enumeration-39
Bitmask-37
Queue-33
Memoization-32
Topological Sort-31
Geometry-30
Segment Tree-27
Game Theory-24
Hash Function-24
Binary Indexed Tree-21
Interactive-18
Data Stream-17
String Matching-17
Rolling Hash-17
Shortest Path-16
Number Theory-16
Combinatorics-15
Randomized-12
Monotonic Queue-9
Iterator-9
Merge Sort-9
Concurrency-9
Doubly-Linked List-8
Brainteaser-8
Probability and Statistics-7
Quickselect-7
Bucket Sort-6
Suffix Array-6
Minimum Spanning Tree-5
Counting Sort-5
Shell-4
Line Sweep-4
Reservoir Sampling-4
Eulerian Circuit-3
Radix Sort-3
Strongly Connected Componen-t2
Rejection Sampling-2
Biconnected Component-1