We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 5f48b51 commit 2d10c37Copy full SHA for 2d10c37
C++/rank-teams-by-votes.cpp
@@ -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