-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
49 lines (48 loc) · 1.3 KB
/
Program.cs
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
using System;
using System.Linq;
namespace RankTeamsByVotes
{
class Program
{
static void Main(string[] args)
{
string[] votes = {"ABC", "ACB", "ABC", "ACB", "ACB"};
Console.WriteLine(RankTeams(votes));
}
static string RankTeams(string[] votes)
{
var rank = new int[26][];
for (int i = 0; i < rank.Length; i++)
{
rank[i] = new int[27];
rank[i][^1] = i + 1;
}
foreach (var vote in votes)
{
for (int i = 0; i < vote.Length; i++)
rank[vote[i] - 'A'][i]++;
}
foreach (var team in rank)
{
if (team.Count(x => x == 0) == 26)
team[26] = -1;
}
Array.Sort(rank, (a, b) =>
{
for (int i = 0; i < 26; i++)
{
if (a[i] != b[i])
return b[i] - a[i];
}
return a[26] - b[26];
});
var res = "";
foreach (var team in rank)
{
if (team[26] != -1)
res += (char) (team[26] - 1 + 'A');
}
return res;
}
}
}