-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathrelative-ranks.js
More file actions
42 lines (34 loc) · 785 Bytes
/
relative-ranks.js
File metadata and controls
42 lines (34 loc) · 785 Bytes
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Source : https://leetcode.com/problems/relative-ranks/
// Author : Han Zichi
// Date : 2017-02-07
/**
* @param {number[]} nums
* @return {string[]}
*/
var findRelativeRanks = function(nums) {
let res = [];
nums.forEach((item, index) => {
res.push({
index: index,
score: item,
rank: null
});
});
res.sort((a, b) => (b.score - a.score));
for (let i = 0, len = res.length; i < len; i++) {
if (i === 0)
res[i].rank = "Gold Medal";
else if (i === 1)
res[i].rank = "Silver Medal";
else if (i === 2)
res[i].rank = "Bronze Medal";
else
res[i].rank = (i + 1) + '';
}
res.sort((a, b) => (a.index - b.index));
let ans = [];
res.forEach((item) => {
ans.push(item.rank);
});
return ans;
};