Skip to content

Commit 2d10c37

Browse files
authored
Create rank-teams-by-votes.cpp
1 parent 5f48b51 commit 2d10c37

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

C++/rank-teams-by-votes.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time: O(m * (n + mlogm)), n is the number of votes
2+
// , m is the length of vote
3+
// Space: O(m^2)
4+
5+
class Solution {
6+
public:
7+
string rankTeams(vector<string>& votes) {
8+
unordered_map<char, vector<int>> count;
9+
for (const auto& v : votes[0]) {
10+
count[v] = vector<int>(votes[0].size());
11+
count[v].emplace_back(static_cast<int>(v));
12+
}
13+
for (const auto& vote : votes) {
14+
for (int i = 0; i < vote.length(); ++i) {
15+
--count[vote[i]][i];
16+
}
17+
}
18+
sort(begin(votes[0]), end(votes[0]),
19+
[&count](const auto& a, const auto& b) {
20+
return count[a] < count[b];
21+
});
22+
return votes[0];
23+
}
24+
};

0 commit comments

Comments
 (0)