1900. The Earliest and Latest Rounds Where Players Compete
Description
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
1competes against player7. - Player
2competes against player6. - Player
4automatically advances to the next round.
- Player
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.
Example 1:
Input: n = 11, firstPlayer = 2, secondPlayer = 4 Output: [3,4] Explanation: One possible scenario which leads to the earliest round number: First round: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 Second round: 2, 3, 4, 5, 6, 11 Third round: 2, 3, 4 One possible scenario which leads to the latest round number: First round: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 Second round: 1, 2, 3, 4, 5, 6 Third round: 1, 2, 4 Fourth round: 2, 4
Example 2:
Input: n = 5, firstPlayer = 1, secondPlayer = 5 Output: [1,1] Explanation: The players numbered 1 and 5 compete in the first round. There is no way to make them compete in any other round.
Constraints:
2 <= n <= 281 <= firstPlayer < secondPlayer <= n
Solutions
Solution 1: Memoization + Binary Enumeration
Thinking
Simulating every remaining pairing each round branches on \(\lfloor n/2\rfloor\) matches. With \(n\le 28\) the raw tree is huge and many prefixes share the same leftover order.
Only the two designated players matter for the answer: other outcomes merely change their next-round ranks and the leftover size. The state therefore collapses to \((l,r,n)\).
We binary-enumerate winners in the first half, force \(l\) and \(r\) to advance, remap survivors in order, and recurse. Memoizing the triple yields the earliest and latest meeting rounds by taking min and max plus one.
We define a function \(\text{dfs}(l, r, n)\), which represents the earliest and latest rounds where players numbered \(l\) and \(r\) compete among \(n\) players in the current round.
The execution logic of function \(\text{dfs}(l, r, n)\) is as follows:
- If \(l + r = n - 1\), it means the two players compete in the current round, return \([1, 1]\).
- If \(f[l][r][n] \neq 0\), it means this state has been calculated before, directly return the result.
- Initialize the earliest round number as positive infinity and the latest round number as negative infinity.
- Calculate the number of players in the first half of the current round \(m = n / 2\).
- Enumerate all possible winner combinations of the first half (using binary enumeration), for each combination:
- Determine which players win based on the current combination.
- Determine the positions of players numbered \(l\) and \(r\) in the current round.
- Count the positions of players numbered \(l\) and \(r\) among the remaining players, denoted as \(a\) and \(b\), and the total number of remaining players \(c\).
- Recursively call \(\text{dfs}(a, b, c)\) to get the earliest and latest round numbers for the current state.
- Update the earliest and latest round numbers.
- Store the calculation result in \(f[l][r][n]\) and return the earliest and latest round numbers.
The answer is \(\text{dfs}(\text{firstPlayer} - 1, \text{secondPlayer} - 1, n)\).
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 | |
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 | |
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 62 | |
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 62 63 64 65 66 67 68 | |
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 62 63 64 65 | |