[LeetCode] Rank Teams by Votes

1366. Rank Teams by Votes

In a special ranking system, each voter gives a rank from highest to lowest to all teams participated in the competition.

The ordering of teams is decided by who received the most position-one votes. If two or more teams tie in the first position, we consider the second position to resolve the conflict, if they tie again, we continue this process until the ties are resolved. If two or more teams are still tied after considering all positions, we rank them alphabetically based on their team letter.

Given an array of strings votes which is the votes of all voters in the ranking systems. Sort all teams according to the ranking system described above.

Return a string of all teams sorted by the ranking system.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
string rankTeams(vector<string>& votes) {
vector<vector<int>> rank(26, vector<int>(27));
int n = votes[0].length();
for(auto& ch : votes[0])
rank[ch-'A'][26] = ch;
for(auto& v : votes) {
for(int i = 0; i < n; i++)
--rank[v[i]-'A'][i];
}
sort(begin(rank), end(rank));
string res = "";
for(int i = 0; i < n; i++) {
res.push_back(rank[i][26]);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/19/PS/LeetCode/rank-teams-by-votes/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.