-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRank_Teams_By_Votes.cpp
71 lines (60 loc) · 1.67 KB
/
Rank_Teams_By_Votes.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Leetcode Problem Link :
https://leetcode.com/problems/rank-teams-by-votes/
*/
class Solution {
public:
map<char, map<int, int>> mp;
map<char, bool> visited;
char getWinner(int teams) {
set<char> st;
auto it = mp.begin();
while (it != mp.end()) {
if (visited.find(it->first) == visited.end()){
st.insert(it->first);
}
++it;
}
for (int i = 0; i < teams; i++){
priority_queue<pair<int, char>> pq;
auto it = mp.begin();
while (it != mp.end()) {
if (st.find(it->first) != st.end()) {
pq.push({it->second[i], it->first});
}
++it;
}
st.clear();
int mx = pq.top().first;
while(!pq.empty()) {
int x = pq.top().first;
char c = pq.top().second;
pq.pop();
if(x == mx) {
st.insert(c);
} else {
break;
}
}
if(st.size() == 1) {
break;
}
}
visited[*st.begin()] = true;
return *st.begin();
}
string rankTeams(vector<string>& votes) {
int n = votes.size();
string ans = "";
int m = votes[0].length();
for(int i = 0; i < n; i++) {
for(int j = 0; j < votes[i].length(); j++) {
mp[votes[i][j]][j]++;
}
}
for(int i = 0; i < m; i++) {
ans+=getWinner(m);
}
return ans;
}
};