The Earliest and Latest Rounds Where Players Compete Problem
Description
LeetCode Problem 1900.
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 i^th player from the front of the row competes against the i^th 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
Solution
Approach: Memoized Search over Round Outcomes
Define dfs(l, r, n): with n players left, where the two special players sit at 0-indexed positions l and r (with l < r), return the earliest and latest round in which they meet.
If l + r = n - 1 they are paired against each other in this round, so the result is [1, 1]. Otherwise, the first n / 2 players are paired with the last n / 2 (the middle player, if any, advances). Enumerate every outcome of the matches not involving the two special players with a bitmask over the first half, since either player of a pair may win. For each outcome, count how many surviving players stand before l, between l and r, and in total; those counts give the new positions a, b and size c. Recurse on dfs(a, b, c) and add one round. The earliest answer is the minimum over outcomes and the latest is the maximum; cache the results per state.
The answer is dfs(firstPlayer - 1, secondPlayer - 1, n).
Complexity: the state count is small because n is at most 28 and roughly halves each round, so the memoized search is fast enough; the bitmask enumeration costs up to 2n/2 per state.
from functools import lru_cache
@lru_cache(None)
def dfs(l: int, r: int, n: int):
if l + r == n - 1:
return [1, 1]
res = [float("inf"), float("-inf")]
m = n >> 1
for i in range(1 << m):
win = [False] * n
for j in range(m):
if i >> j & 1:
win[j] = True
else:
win[n - 1 - j] = True
if n & 1:
win[m] = True
win[n - 1 - l] = win[n - 1 - r] = False
win[l] = win[r] = True
a = b = c = 0
for j in range(n):
if j == l:
a = c
if j == r:
b = c
if win[j]:
c += 1
x, y = dfs(a, b, c)
res[0] = min(res[0], x + 1)
res[1] = max(res[1], y + 1)
return res
class Solution:
def earliestAndLatest(
self, n: int, firstPlayer: int, secondPlayer: int
) -> List[int]:
return dfs(firstPlayer - 1, secondPlayer - 1, n)