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, theearliest possible round number and the
latest possible round number in which these two players will compete against each other, respectively.
Input: n =11, firstPlayer =2, secondPlayer =4Output: [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,11Second round:2,3,4,5,6,11Third round:2,3,4One possible scenario which leads to the latest round number:First round:1,2,3,4,5,6,7,8,9,10,11Second round:1,2,3,4,5,6Third round:1,2,4Fourth round:2,4
Input: n =5, firstPlayer =1, secondPlayer =5Output: [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.
#include<vector>#include<algorithm>#include<tuple>usingnamespace std;
pair<int,int> dfs(int n, int a, int b, vector<vector<vector<pair<int,int>>>>& memo) {
if (a > b) swap(a, b);
if (memo[n][a][b].first !=-1) return memo[n][a][b];
if (a +1== n - b) return memo[n][a][b] = {1, 1};
int minr =100, maxr =0;
for (int la =0; la <= a; ++la) {
for (int lb =0; lb <= n -1- b; ++lb) {
int na = la + lb;
int nb = na + (b - a -1);
int nn = (n +1) /2;
auto [ear, lat] = dfs(nn, na, nb, memo);
minr = min(minr, ear +1);
maxr = max(maxr, lat +1);
}
}
return memo[n][a][b] = {minr, maxr};
}
vector<int> earliestAndLatest(int n, int firstPlayer, int secondPlayer) {
vector<vector<vector<pair<int,int>>>> memo(n+1, vector<vector<pair<int,int>>>(n, vector<pair<int,int>>(n, {-1,-1})));
auto [ear, lat] = dfs(n, firstPlayer-1, secondPlayer-1, memo);
return {ear, lat};
}
import java.util.*;
classSolution {
privateint[][][] minMemo, maxMemo;
publicint[]earliestAndLatest(int n, int firstPlayer, int secondPlayer) {
minMemo =newint[n+1][n][n];
maxMemo =newint[n+1][n][n];
for (int[][] mm : minMemo) for (int[] m : mm) Arrays.fill(m, -1);
for (int[][] mm : maxMemo) for (int[] m : mm) Arrays.fill(m, -1);
return dfs(n, firstPlayer-1, secondPlayer-1);
}
privateint[]dfs(int n, int a, int b) {
if (a > b) return dfs(n, b, a);
if (minMemo[n][a][b]!=-1) returnnewint[]{minMemo[n][a][b], maxMemo[n][a][b]};
if (a + 1 == n - b) returnnewint[]{1, 1};
int minr = 100, maxr = 0;
for (int la = 0; la <= a; ++la) {
for (int lb = 0; lb <= n - 1 - b; ++lb) {
int na = la + lb;
int nb = na + (b - a - 1);
int nn = (n + 1) / 2;
int[] res = dfs(nn, na, nb);
minr = Math.min(minr, res[0]+ 1);
maxr = Math.max(maxr, res[1]+ 1);
}
}
minMemo[n][a][b]= minr;
maxMemo[n][a][b]= maxr;
returnnewint[]{minr, maxr};
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from functools import lru_cache
defearliestAndLatest(n: int, firstPlayer: int, secondPlayer: int) -> [int, int]:
@lru_cache(None)
defdfs(n, a, b):
if a > b: a, b = b, a
if a +1== n - b:
return1, 1 minr, maxr =100, 0for la in range(a+1):
for lb in range(n-b):
na = la + lb
nb = na + (b - a -1)
nn = (n +1) //2 ear, lat = dfs(nn, na, nb)
minr = min(minr, ear +1)
maxr = max(maxr, lat +1)
return minr, maxr
return list(dfs(n, firstPlayer-1, secondPlayer-1))