Skip to content

Commit 4c8f455

Browse files
committed
Create 1366.通过投票对团队排名.js
1 parent ad7fb9d commit 4c8f455

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

1366.通过投票对团队排名.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {string[]} votes
3+
* @return {string}
4+
*/
5+
var rankTeams = function(votes) {
6+
if (votes.length === 1) {
7+
return votes[0];
8+
}
9+
10+
let result = new Array(26);
11+
for (let i = 0; i < result.length; i++) {
12+
result[i] = new Array(26).fill(0);
13+
}
14+
for (let i = 0; i < votes.length; i++) {
15+
for (let j = 0; j < votes[i].length; j++) {
16+
const code = votes[i].charCodeAt(j) - 65;
17+
result[code][j]++;
18+
}
19+
}
20+
21+
result = result.map((list, i) => ({ k: String.fromCharCode(i + 65), v: list }));
22+
result.sort((a, b) => {
23+
for (let i = 0; i < 26; i++) {
24+
if (a.v[i] != b.v[i]) {
25+
return b.v[i] - a.v[i];
26+
}
27+
}
28+
return a.k < b.k ? -1 : 1;
29+
});
30+
31+
return result.map(({k}) => k).slice(0, votes[0].length).join('');
32+
};

0 commit comments

Comments
 (0)