Skip to content

Commit fc55abf

Browse files
committed
Relative Ranks
1 parent a9ed72d commit fc55abf

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Source : https://leetcode.com/problems/relative-ranks/
2+
// Author : Han Zichi
3+
// Date : 2017-02-07
4+
5+
/**
6+
* @param {number[]} nums
7+
* @return {string[]}
8+
*/
9+
var findRelativeRanks = function(nums) {
10+
let res = [];
11+
12+
nums.forEach((item, index) => {
13+
res.push({
14+
index: index,
15+
score: item,
16+
rank: null
17+
});
18+
});
19+
20+
res.sort((a, b) => (b.score - a.score));
21+
22+
for (let i = 0, len = res.length; i < len; i++) {
23+
if (i === 0)
24+
res[i].rank = "Gold Medal";
25+
else if (i === 1)
26+
res[i].rank = "Silver Medal";
27+
else if (i === 2)
28+
res[i].rank = "Bronze Medal";
29+
else
30+
res[i].rank = (i + 1) + '';
31+
}
32+
33+
res.sort((a, b) => (a.index - b.index));
34+
35+
let ans = [];
36+
37+
res.forEach((item) => {
38+
ans.push(item.rank);
39+
});
40+
41+
return ans;
42+
};

0 commit comments

Comments
 (0)