-
Notifications
You must be signed in to change notification settings - Fork 1
/
LC_1366.cpp
43 lines (38 loc) · 1.18 KB
/
LC_1366.cpp
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
34
35
36
37
38
39
40
41
42
43
class Solution {
public:
static bool cmp(const pair<char, vector<int>>& a, const pair<char, vector<int>>& b){
for(int i = 0; i < a.second.size(); i++)
{
if(a.second[i] > b.second[i])
return true;
if(a.second[i] < b.second[i])
return false;
}
return a.first < b.first;
}
string rankTeams(vector<string>& votes) {
if(votes.size() == 1)
return votes.back();
unordered_map<char, vector<int>> mp;
vector<int> init(votes[0].size(), 0);
for(int i = 0; i < votes[0].size(); i++)
mp[votes[0][i]] = init;
for(int i = 0; i < votes.size(); i++){
for(int j = 0; j < votes[i].size(); j++){
mp[votes[i][j]][j]++;
}
}
pair<char, vector<int>> temp;
vector<pair<char, vector<int>>> helper;
for(auto it : mp)
{
temp = make_pair(it.first, it.second);
helper.push_back(temp);
}
sort(helper.begin(), helper.end(), cmp);
string ret = "";
for(auto p : helper)
ret.push_back(p.first);
return ret;
}
};