[C++] Recursion optimized 24 ms 32 MB
\n#pragma GCC optimize("Ofast")\n#pragma GCC optimize("unroll-loops")\n#pragma GCC optimize("inline")\n\nclass Solution {\npublic:\n    int f,s,cur_level,mi,ma;
```\n#pragma GCC optimize("Ofast")\n#pragma GCC optimize("unroll-loops")\n#pragma GCC optimize("inline")\n\nclass Solution {\npublic:\n    int f,s,cur_level,mi,ma;\n    vector<vector<int>> cur;\n    vector<int> prev;\n    //recurse all games\n    void games(vector<int> & ans, int pos){\n        if(prev.size()%2==0){\n            if(pos==prev.size()/2){\n                vector<int> temp=ans;\n                //sort before adding possible permute\n                sort(temp.begin(),temp.end());\n                if(temp.size()>1)cur.push_back(temp);\n                return;\n            }\n        }\n        else{\n            if(pos==prev.size()/2){\n                ans.push_back(prev[pos]);\n                vector<int> temp=ans;\n                sort(temp.begin(),temp.end());\n                if(temp.size()>1)cur.push_back(temp);\n                ans.pop_back();\n                return;\n            }\n        }\n        \n        bool f1=false,s1=false;\n        if(prev[pos]==f  || prev[prev.size()-1-pos]==f){\n            f1=true;\n        }\n        if(prev[pos]==s || prev[prev.size()-1-pos]==s){\n            s1=true;\n        }\n        if(f1&&s1){\n            //no need to followup children games\n            mi=min(mi,cur_level);\n            ma=max(ma,cur_level);\n            return;\n        }\n        else \n        {\n            if(f1 || s1){\n                if(f1){\n                    ans.push_back(f);\n                    games(ans,pos+1);\n                    ans.pop_back();\n                }\n                else{\n                    ans.push_back(s);\n                    games(ans,pos+1);\n                    ans.pop_back();\n                }\n            }\n            else{\n                //optimization: don\'t care about outcome if current two players lie on same side of firstPlayer and secondPlayer\n                if(((prev[pos]<f)&&(prev[prev.size()-1-pos]<f)) || ((prev[pos]>s)&&(prev[prev.size()-1-pos]>s)) || ((prev[pos]>f)&&(prev[prev.size()-1-pos]<s))){\n                    ans.push_back(prev[pos]);\n                    games(ans,pos+1);\n                    ans.pop_back();\n                }\n                //recurse for both possible outcome\n                else {\n                    ans.push_back(prev[pos]);\n                    games(ans,pos+1);\n                    ans.pop_back();\n                    ans.push_back(prev[prev.size()-1-pos]);\n                    games(ans,pos+1);\n                    ans.pop_back();\n                }\n                \n                return;\n            }\n        }\n        \n    }\n    \n    vector<int> earliestAndLatest(int n, int firstPlayer, int secondPlayer) {\n        mi=100000;\n        ma=0;\n        cur_level=1;\n        f = firstPlayer;\n        s = secondPlayer;\n        vector<int> ans;\n        vector<vector<int>> prev_games;\n        vector<int> init;\n        for(int i=1;i<=n;i++)init.push_back(i);\n        //initial configuration\n        prev_games.push_back(init);\n        while(prev_games.size()){\n            cur = vector<vector<int>>();\n            vector<int> temp,temp2;\n            for(auto i:prev_games){\n                prev=i;\n                games(temp,0);\n            }\n            //keep only unique order of firstPlayer and secondPlayer\n            prev_games = vector<vector<int>>();\n            unordered_map<int,int> m;\n            for(auto i:cur){\n                long int num=0;\n                for(auto j:i){\n                    num*=2;\n                    if(j==f ||j==s)num++;\n                }\n                if(m[num]==0){\n                    m[num]++;\n                    prev_games.push_back(i);\n                }\n                \n            }\n            \n            cur_level++;\n        }    \n        ans.push_back(mi);\n        ans.push_back(ma);\n        return ans;\n    }\n};\n```
[]