Skip to content

Latest commit

 

History

History
125 lines (108 loc) · 4.92 KB

README.md

File metadata and controls

125 lines (108 loc) · 4.92 KB

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
    <ul>
    	<li>Player <code>1</code> competes against player <code>7</code>.</li>
    	<li>Player <code>2</code> competes against player <code>6</code>.</li>
    	<li>Player <code>4</code> automatically advances to the next round.</li>
    </ul>
    </li>
    

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 <= 28
  • 1 <= firstPlayer < secondPlayer <= n

Companies:
Google

Related Topics:
Dynamic Programming

Solution 1.

// OJ: https://leetcode.com/problems/the-earliest-and-latest-rounds-where-players-compete
// Author: github.com/lzl124631x
// Time: O()
// Space: O()
class Solution {
    unordered_map<int, vector<int>> m;
    int n, first, second;
    void dfs(int mask, int i, int j, vector<int> &ans) {
        if (i >= j) {
            ans.push_back(mask);
            return;
        }
        while (i < j && !(mask >> i & 1) ) ++i;
        while (i < j && !(mask >> j & 1) ) --j;
        if (i < j) {
            if (i != first && i != second )
                dfs(mask & ~(1 << i), i + 1, j - 1, ans);
            if (j != first && j != second)
                dfs(mask & ~(1 << j), i + 1, j - 1, ans);
            ++i;
            --j;
        } else {
            ans.push_back(mask);
        }
    }
    vector<int> gen(int mask) {
        vector<int> ans;
        dfs(mask, 0, n - 1, ans);
        return ans;
    }
    vector<int> dp(int mask) {
        if (m.count(mask)) return m[mask];
        int L = 0, R = 0;
        for (int i = 0; i < first; ++i) {
            if (mask >> i & 1) ++L;
        }
        for (int i = n - 1; i > second; --i) {
            if (mask >> i & 1) ++R;
        }
        if (L == R) return {1,1};
        auto next = gen(mask);
        int mn = INT_MAX, mx = 0;
        for (int nmask : next) {
            auto v = dp(nmask);
            mn = min(mn, v[0]);
            mx = max(mx, v[1]);
        }
        return m[mask] = {mn + 1,mx + 1};
    }
public:
    vector<int> earliestAndLatest(int n, int first, int second) {
        this->n = n;
        this->first = first - 1;
        this->second = second - 1;
        int mask = 0;
        for (int i = 0; i < n; ++i) mask |= 1 << i;
        return dp(mask);
    }
};