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`

player from the front of the row competes against the ^{th}`i`

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.^{th}

- 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.

- 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 = 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, 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 = 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.

**Constraints:**

`2 <= n <= 28`

`1 <= firstPlayer < secondPlayer <= n`

**Solution 1: Simulation using recursion**

All possible paths,

Time complexity: O(n^{2}*2^{n})

Space complexity: O(logn)

dfs(s, i, j, d) := let i battle with j at round d, given s (binary mask of dead players).

## C++

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Author: Huahua class Solution { public: vector<int> earliestAndLatest(int n, int a, int b) { vector<int> ans{INT_MAX, INT_MIN}; function<void(int, int, int, int)> dfs = [&](int s, int i, int j, int d) { while (i < j && s >> i & 1) ++i; while (i < j && s >> j & 1) --j; if (i >= j) { return dfs(s, 1, n, d + 1); } else if (i == a && j == b) { ans[0] = min(ans[0], d); ans[1] = max(ans[1], d); } else { if (i != a && i != b) dfs(s | (1 << i), i + 1, j - 1, d); if (j != a && j != b) dfs(s | (1 << j), i + 1, j - 1, d); } }; dfs(0, 1, n, 1); return ans; } }; |

请尊重作者的劳动成果，转载请注明出处！花花保留对文章／视频的所有权利。

如果您喜欢这篇文章／视频，欢迎您捐赠花花。

If you like my articles / videos, donations are welcome.

## Be First to Comment