Skip to content

Commit ba609f6

Browse files
authored
Create 1366-rank-teams-by-votes.js
1 parent d2abe01 commit ba609f6

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

1366-rank-teams-by-votes.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {string[]} votes
3+
* @return {string}
4+
*/
5+
const rankTeams = function(votes) {
6+
if (votes.length === 1) return votes[0];
7+
const score = new Map(votes[0].split('').map(c => [c, new Array(votes[0].length).fill(0)]));
8+
for (s of votes) {
9+
for (let i = 0; i < s.length; i++) {
10+
score.get(s[i])[i]++;
11+
}
12+
}
13+
return votes[0].split('').sort((a,b) => {
14+
for (let i = 0; i < votes[0].length; i++) {
15+
if (score.get(a)[i] > score.get(b)[i]) return -1;
16+
if (score.get(a)[i] < score.get(b)[i]) return 1;
17+
}
18+
return a < b ? -1 : 1;
19+
}).join('');
20+
};

0 commit comments

Comments
 (0)