O(n^3) C++ solution, <200ms for n=900, hard to understand
\n#define N 28\nstatic array<array<array<bool, N>, N>, N+1> mem;\nclass Solution {\n    int mn, mx;\n    void dfs(int n, int p1, int p2, int d) {\n        if (n
```\n#define N 28\nstatic array<array<array<bool, N>, N>, N+1> mem;\nclass Solution {\n    int mn, mx;\n    void dfs(int n, int p1, int p2, int d) {\n        if (n == 1 || mem[n][p1][p2])\n            return;\n        mem[n][p1][p2] = true;\n        int q1 = n - p1 - 1, q2 = n - p2 - 1;\n        if (p1 == q2) {\n            mn = min(mn, d);\n            mx = max(mx, d);\n            return;\n        }\n        int m = n + 1 >> 1;\n        for (int i = max(0, p1 - q1 >> 1);\n             i <= p1 - (q1 < p1) - (q2 < p1) - max(0, p1 - q1 - 1 >> 1);\n             i++)\n            for (int j = max(0, min(q1, p2) - max(q2, p1) >> 1);\n                 j <= p2 - p1 - 1 - (q1 > p1 && q1 < p2) - (q2 > p1 && q2 < p2) - max(0, min(q1, p2) - max(q2, p1) - 1 >> 1);\n                 j++)\n                if (m - i - j - 2 >= max(0, q2 - p2 >> 1)\n                    && m - i - j - 2 <= q2 - (q1 > p2) - (q2 > p2) - max(0, q2 - p2 - 1 >> 1))\n                    dfs(m, i, i + j + 1, d + 1);\n    }\npublic:\n    vector<int> earliestAndLatest(int n, int first, int second) {\n        mn = n, mx = 0, mem = {};\n        dfs(n, first - 1, second - 1, 1);\n        return {mn, mx};\n    }\n};\n```
['Depth-First Search', 'C']