-
Notifications
You must be signed in to change notification settings - Fork 0
/
rank_teams.cpp
53 lines (46 loc) · 1.19 KB
/
rank_teams.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
// 1366. Rank Teams by Votes: https://leetcode.com/problems/rank-teams-by-votes
// Author: [email protected]
#include <stdio.h>
#include <algorithm>
#include <string>
#include <vector>
using std::string;
using std::vector;
class Solution
{
public:
string rankTeams(const vector<string>& votes)
{
const int chr_size = 26;
vector<vector<int>> counts(chr_size, vector<int>(chr_size + 1, 0));
for (const auto chr: votes[0])
{
counts[chr - 'A'][chr_size] = chr;
}
for (const auto& vote: votes)
{
for (int i = 0; i < vote.size(); i++)
{
counts[vote[i] - 'A'][i] -= 1;
}
}
// Sort votes
std::sort(counts.begin(), counts.end());
string rank;
for (const auto& row: counts)
{
if (row[chr_size] != 0)
{
rank += (char)row[chr_size];
}
}
return rank;
}
};
int main(int argc, char* argv[])
{
vector<string> votes = {"BCA", "CAB", "CBA", "ABC", "ACB", "BAC"};
string rank = Solution().rankTeams(votes);
printf("Output: %s\n", rank.c_str());
return 0;
}