-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
58 lines (55 loc) · 1.56 KB
/
Solution.cs
File metadata and controls
58 lines (55 loc) · 1.56 KB
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
public class Solution
{
int min = int.MaxValue;
int max = 0;
public int[] EarliestAndLatest(int n, int firstPlayer, int secondPlayer)
{
List<int> players = [];
for (int i = 1; i <= n; i++) players.Add(i);
Solve(firstPlayer, secondPlayer, 1, players);
return [min, max];
}
void Solve(int firstPlayer, int secondPlayer, int round, List<int> players)
{
int index1 = players.IndexOf(firstPlayer);
int index2 = players.IndexOf(secondPlayer);
int n = players.Count;
if ((n - index2 - 1) == index1)
{
min = Math.Min(min, round);
max = Math.Max(max, round);
return;
}
int maxBitMask = 1 << (n / 2);
for (int bitMask = 0; bitMask < maxBitMask; bitMask++)
{
List<int> nextPlayers = NextRound(players, bitMask);
if (nextPlayers.Contains(firstPlayer) && nextPlayers.Contains(secondPlayer))
{
Solve(firstPlayer, secondPlayer, round + 1, nextPlayers);
}
}
}
List<int> NextRound(List<int> players, int bitMask)
{
List<int> ret = [];
int n = players.Count;
if (n % 2 == 1)
{
ret.Add(players[n / 2]);
}
for (int i = 0; i < n / 2; i++)
{
if ((bitMask & (1 << i)) != 0)
{
ret.Add(players[i]);
}
else
{
ret.Add(players[n - i - 1]);
}
}
ret.Sort();
return ret;
}
}