C++ | DP with bit masking
\nclass Solution {\npublic:\n    vector<int> earliestAndLatest(int n_, int firstPlayer, int secondPlayer) {\n        vector<int> v(n_);\n        firstPlayer--;\
```\nclass Solution {\npublic:\n    vector<int> earliestAndLatest(int n_, int firstPlayer, int secondPlayer) {\n        vector<int> v(n_);\n        firstPlayer--;\n        secondPlayer--;\n        for(int i=0;i<n_;i++)\n            v[i]=i;\n        map<int,pair<int,int>> my;//min,max\n        std::function<pair<int,int>(int,int,int)> minTimes = [&](int hash, int val, int n){\n            if(n<=1){\n                pair<int,int> p = {1000,0};\n                return p;\n            }\n            if(my.find(hash)!=my.end())\n                return my[hash];\n            int size = n;\n            int w[size];\n            int times = n/2;\n            int j = 0;\n            for(int i=0;i<n_;i++){\n                if(!((1<<i)&hash)){\n                    w[j++]=i;\n                }\n            }\n            int i = 0;\n            j = n-1;\n            bool check = false;\n            while(i<j){\n                if((v[w[i]]==firstPlayer && v[w[j]]==secondPlayer) || (v[w[j]]==firstPlayer && v[w[i]]==secondPlayer)){\n                    check = true;\n                    break;\n                }\n                i++;\n                j--;\n            }\n            if(check){\n                pair<int,int> p = {val,val};\n                return p;\n            }\n            // L R -> L wins => 1\n            // L R -> R wins => 0\n            int upto = (1<<times);\n            int ans1 = 1000;\n            int ans2 = 0;\n            for(int k=0;k<upto;k++){\n                i=0,j=n-1;\n                int track = 0;\n                for(int p=0;p<times;p++){\n                    int t = (1<<p);\n                    if(v[w[i]]==firstPlayer)\n                        track^=(1<<v[w[i]]);\n                    else if(v[w[i]]==secondPlayer)\n                        track^=(1<<v[w[i]]);\n                    else if(v[w[j]]==firstPlayer)\n                        track^=(1<<v[w[j]]);\n                    else if(v[w[j]]==secondPlayer)\n                        track^=(1<<v[w[j]]);\n                    else{\n                        if(t&k)\n                            track^=(1<<v[w[i]]);\n                        else\n                            track^=(1<<v[w[j]]);\n                    }\n                    i++;\n                    j--;\n                }\n                if(n&1)\n                    track^=(1<<v[w[times]]);\n                int bit_val = (1<<n_)-1;\n                bit_val^=track;\n                track=bit_val;\n                pair<int,int> p = minTimes(track,val+1,(n+1)/2);\n                ans1=min(ans1,p.first);\n                ans2=max(ans2,p.second);\n            }\n            \n            return my[hash] = {ans1,ans2};\n        };\n        auto p = minTimes(0,1,n_);\n        vector<int> ret={p.first,p.second};\n        return ret;\n    }\n};\n\n\n\n\n```
[]