-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1366.cpp
More file actions
33 lines (31 loc) · 967 Bytes
/
1366.cpp
File metadata and controls
33 lines (31 loc) · 967 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution
{
public:
string rankTeams(vector<string>& votes)
{
int n = votes.size();
// 初始化哈希映射
unordered_map<char, vector<int>> ranking;
for (char vid: votes[0]) {
ranking[vid].resize(votes[0].size());
}
// 遍历统计
for (const string& vote: votes) {
for (int i = 0; i < vote.size(); ++i) {
++ranking[vote[i]][i];
}
}
// 取出所有的键值对
using PCV = pair<char, vector<int>>;
vector<PCV> result(ranking.begin(), ranking.end());
// 排序
sort(result.begin(), result.end(), [](const PCV& l, const PCV& r) {
return l.second > r.second || (l.second == r.second && l.first < r.first);
});
string ans;
for (auto& [vid, rank]: result) {
ans += vid;
}
return ans;
}
};