(C++) 1900. The Earliest and Latest Rounds Where Players Compete
\n\nclass Solution {\npublic:\n    vector<int> earliestAndLatest(int n, int firstPlayer, int secondPlayer) {\n        firstPlayer -= 1, secondPlayer -= 1; \n   
\n```\nclass Solution {\npublic:\n    vector<int> earliestAndLatest(int n, int firstPlayer, int secondPlayer) {\n        firstPlayer -= 1, secondPlayer -= 1; \n        \n        map<int, vector<int>> memo; \n        function<vector<int>(int, int, int, int)> fn = [&](int r, int mask, int i, int j) {\n            if (memo.find(mask) == memo.end()) {\n                if (i >= j) return fn(r+1, mask, 0, n-1); \n                if (!(mask & (1 << i))) return fn(r, mask, i+1, j); \n                if (!(mask & (1 << j))) return fn(r, mask, i, j-1); \n                if ((i == firstPlayer && j == secondPlayer) || (i == secondPlayer && j == firstPlayer)) return vector<int>(2, r); \n                if (i == firstPlayer || i == secondPlayer) return fn(r, mask^(1<<j), i+1, j-1); \n                if (j == firstPlayer || j == secondPlayer) return fn(r, mask^(1<<i), i+1, j-1); \n                else {\n                    vector<int> x = fn(r, mask^(1<<j), i+1, j-1); \n                    vector<int> y = fn(r, mask^(1<<i), i+1, j-1); \n                    memo[mask] = {min(x[0], y[0]), max(x[1], y[1])}; \n                }\n            }\n            return memo[mask]; \n        };\n        \n        return fn(1, (1<<n)-1, 0, n-1); \n    }\n};\n```
['C']