[Java] Dfs + memo solution
state : remaining people, pos of first player, pos of second player\n2. Simulation : Use bitmask to simulate and get the next round\'s standing\n\n\nclass Solut
1. state : remaining people, pos of first player, pos of second player\n2. Simulation : Use bitmask to simulate and get the next round\'s standing\n\n```\nclass Solution {\n    int dp1[][][];\n    int dp2[][][];\n    \n    public int[] earliestAndLatest(int n, int a, int b) {\n        dp1=new int[n+1][n+1][n+1];\n        dp2=new int[n+1][n+1][n+1];\n        for(int i=0;i<dp1.length;i++){\n            for(int j=0;j<dp1[0].length;j++){\n                Arrays.fill(dp1[i][j],-1);\n                Arrays.fill(dp2[i][j],-1);\n            }\n        }\n        int res[]=new int[2];\n        res[0]=dfs1(n,a,b);\n        res[1]=dfs2(n,a,b);\n        return res;\n    }\n    \n    public int dfs1(int n,int pos1,int pos2){\n        if(pos1+pos2==1+n){\n            return 1;//match directly\n        }\n        if(dp1[n][pos1][pos2]!=-1)return dp1[n][pos1][pos2];\n        \n        int res=Integer.MAX_VALUE;\n        int match=(n+1)/2;\n        \n        \n        for(int state=0;state<(1<<match);state++){\n            List<Integer>list=new ArrayList<>();\n            boolean a=false,b=false;\n            for(int i=0;i<match;i++){\n                if((state&(1<<i))!=0){\n                    list.add(i+1);\n                    if(i+1==pos1)a=true;\n                    if(i+1==pos2)b=true;\n                }\n                else{\n                    list.add(n-i);\n                    if(n-i==pos1)a=true;\n                    if(n-i==pos2)b=true;\n                }\n            }\n            if(a&b){\n                int p1=-1,p2=-1;\n                Collections.sort(list);\n                for(int i=0;i<list.size();i++){\n                    if(list.get(i)==pos1)p1=i+1;\n                    if(list.get(i)==pos2)p2=i+1;\n                }\n                res=Math.min(res,1+dfs1(match,p1,p2));\n            }\n            \n        }\n        \n        dp1[n][pos1][pos2]=res;\n        return res;\n    }\n    \n    \n    public int dfs2(int n,int pos1,int pos2){//mn\n        if(pos1+pos2==1+n){\n            return 1;//match directly\n        }\n        if(dp2[n][pos1][pos2]!=-1)return dp2[n][pos1][pos2];\n        \n        int res=Integer.MIN_VALUE;\n        int match=(n+1)/2;\n        \n        \n        for(int state=0;state<(1<<match);state++){\n            List<Integer>list=new ArrayList<>();\n            boolean a=false,b=false;\n            for(int i=0;i<match;i++){\n                if((state&(1<<i))!=0){\n                    list.add(i+1);\n                    if(i+1==pos1)a=true;\n                    if(i+1==pos2)b=true;\n                }\n                else{\n                    list.add(n-i);\n                    if(n-i==pos1)a=true;\n                    if(n-i==pos2)b=true;\n                }\n            }\n            if(a&b){\n                int p1=-1,p2=-1;\n                Collections.sort(list);\n                for(int i=0;i<list.size();i++){\n                    if(list.get(i)==pos1)p1=i+1;\n                    if(list.get(i)==pos2)p2=i+1;\n                }\n                res=Math.max(res,1+dfs2(match,p1,p2));\n            }\n            \n        }\n        \n        dp2[n][pos1][pos2]=res;\n        return res;\n    }\n}\n\n\n```
[]