Skip to content

Commit c7798ef

Browse files
authored
Update 1366-rank-teams-by-votes.js
1 parent ba609f6 commit c7798ef

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

1366-rank-teams-by-votes.js

+29
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1+
/**
2+
* @param {string[]} votes
3+
* @return {string}
4+
*/
5+
const rankTeams = function(votes) {
6+
const hash = {}
7+
const l = votes[0].length
8+
for(let vote of votes) {
9+
for(let i = 0; i < l; i++) {
10+
const ch = vote[i]
11+
if(hash[ch] == null) hash[ch] = Array(l).fill(0)
12+
hash[ch][i]++
13+
}
14+
}
15+
const keys = Object.keys(hash)
16+
keys.sort((a, b) => {
17+
for(let i = 0; i < l; i++) {
18+
if(hash[a][i] !== hash[b][i]) {
19+
return hash[b][i] - hash[a][i]
20+
}
21+
}
22+
return a === b ? 0 : (a < b ? -1 : 1)
23+
})
24+
25+
return keys.join('')
26+
};
27+
28+
// another
29+
130
/**
231
* @param {string[]} votes
332
* @return {string}

0 commit comments

Comments
 (0)