👍Runtime 0 ms Beats 100.00%
Code\n\nclass Solution {\n    public int[] earliestAndLatest(int n, int firstPlayer, int secondPlayer) {\n        int p1 = Math.min(firstPlayer, secondPlayer);\
# Code\n```\nclass Solution {\n    public int[] earliestAndLatest(int n, int firstPlayer, int secondPlayer) {\n        int p1 = Math.min(firstPlayer, secondPlayer);\n        int p2 = Math.max(firstPlayer, secondPlayer);\n        if (p1 + p2 == n + 1) {\n            // p1 and p2 compete in the first round\n            return new int[]{1, 1};\n        }\n        if (n == 3 || n == 4) {\n            // p1 and p2 must compete in the second round (only two rounds).\n            return new int[]{2, 2};\n        }\n\n        // Flip to make p1 be more closer to left than p2 to right end for convenience\n        if (p1 - 1 > n - p2) {\n            int t = n + 1 - p1;\n            p1 = n + 1 - p2;\n            p2 = t;\n        }\n\n        int m = (n + 1) / 2;\n        int min = n;\n        int max = 1;\n        if (p2 * 2 <= n + 1) {\n            // p2 is in the first half (n odd or even) or exact middle (n odd)\n            int a = p1 - 1;\n            int b = p2 - p1 - 1;\n            // i represents the number of front players in A wins\n            // j represents the number of front players in B wins\n            for (int i = 0; i <= a; i++) {\n                for (int j = 0; j <= b; j++) {\n                    int[] ret = earliestAndLatest(m, i + 1, i + j + 2);\n                    min = Math.min(min, 1 + ret[0]);\n                    max = Math.max(max, 1 + ret[1]);\n                }\n            }\n        } else {\n            // p2 is in the later half (and has >= p1 distance to the end)\n            int p4 = n + 1 - p2;\n            int a = p1 - 1;\n            int b = p4 - p1 - 1;\n            // Group C are players between p4 and p2, (c+1)/2 will advance to the next round.\n            int c = p2 - p4 - 1;\n            for (int i = 0; i <= a; i++) {\n                for (int j = 0; j <= b; j++) {\n                    int[] ret = earliestAndLatest(m, i + 1, i + j + 1 + (c + 1) / 2 + 1);\n                    min = Math.min(min, 1 + ret[0]);\n                    max = Math.max(max, 1 + ret[1]);\n                }\n            }\n        }\n        return new int[]{min, max};\n    }\n}\n```
['Java']