File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Algorithms/Relative Ranks Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments