[LeetCode] The Earliest and Latest Rounds Where Players Compete

1900. The Earliest and Latest Rounds Where Players Compete

There is a tournament where n players are participating. The players are standing in a single row and are numbered from 1 to n based on their initial standing position (player 1 is the first player in the row, player 2 is the second player in the row, etc.).

The tournament consists of multiple rounds (starting from round number 1). In each round, the ith player from the front of the row competes against the ith player from the end of the row, and the winner advances to the next round. When the number of players is odd for the current round, the player in the middle automatically advances to the next round.

  • For example, if the row consists of players 1, 2, 4, 6, 7
  • Player 1 competes against player 7.
  • Player 2 competes against player 6.
  • Player 4 automatically advances to the next round.

After each round is over, the winners are lined back up in the row based on the original ordering assigned to them initially (ascending order).

The players numbered firstPlayer and secondPlayer are the best in the tournament. They can win against any other player before they compete against each other. If any two other players compete against each other, either of them might win, and thus you may choose the outcome of this round.

Given the integers n, firstPlayer, and secondPlayer, return an integer array containing two values, the earliest possible round number and the latest possible round number in which these two players will compete against each other, respectively.

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
59
60
61
class Solution {
unordered_map<bitset<28>, pair<int, int>> dp;
pair<int, int> helper(bitset<28> mask, int& f, int& s, int round, int& n) {
if(dp.count(mask)) return dp[mask];
dp[mask] = {INT_MAX, INT_MIN};
int l = 0, r = n;
unordered_set<bitset<28>> comb{bitset<28>(0)};
while(l <= r) {
while(!mask[l]) l++;
while(!mask[r]) r--;
if(l > r) break;
unordered_set<bitset<28>> ncomb;
if(l == r) {
for(bitset<28> c : comb) {
c[l] = 1;
ncomb.insert(c);
}
swap(comb, ncomb);
break;
}
if(l == f and r == s) {
return dp[mask] = {round, round};
}
for(bitset<28> c : comb) {
if(l == f or l == s or r == f or r== s) {
if(l == f or l == s) {
c[l] = 1;
ncomb.insert(c);
} else {
c[r] = 1;
ncomb.insert(c);
}
} else {
c[l] = 1;
ncomb.insert(c);
c[l] = 0; c[r] = 1;
ncomb.insert(c);
}
}
swap(comb, ncomb);
l++, r--;
}

for(auto& c : comb) {
auto [mi, ma] = helper(c, f, s, round + 1, n);
dp[mask].first = min(dp[mask].first, mi);
dp[mask].second = max(dp[mask].second, ma);
}


return dp[mask];
}
public:
vector<int> earliestAndLatest(int n, int firstPlayer, int secondPlayer) {
bitset<28> bi;
for(int i = 0; i < n; i++) bi[i] = 1;
--firstPlayer; --secondPlayer;
auto [mi, ma] = helper(bi, firstPlayer, secondPlayer, 1, n);
return {mi, ma};
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/01/PS/LeetCode/the-earliest-and-latest-rounds-where-players-compete/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.