Java dp solution
\nclass Solution {\n    int MAX=2000;\n    public int minSwap(int[] A, int[] B) {\n        MAX=A.length+10;\n        if(A.length==1)return 0;\n        int dp[][
```\nclass Solution {\n    int MAX=2000;\n    public int minSwap(int[] A, int[] B) {\n        MAX=A.length+10;\n        if(A.length==1)return 0;\n        int dp[][]=new int[2][A.length]; //[Nonswap,swap]\n        dp[0][0]=0;dp[1][0]=1;\n        for(int i=1;i<A.length;i++){\n            //Non swap\n            int nonA=A[i];\n            int nonB=B[i];\n            int val=MAX;\n            if(nonA>A[i-1]&&nonB>B[i-1]){//not swap pre\n                val=Math.min(val,dp[0][i-1]);\n            }\n            if(nonA>B[i-1]&&nonB>A[i-1]){//swap pre\n                val=Math.min(val,dp[1][i-1]); \n            }\n            dp[0][i]=val;\n            //Swap\n            int swapA=B[i];\n            int swapB=A[i];\n            val=MAX;\n            if(swapA>A[i-1]&&swapB>B[i-1]){\n                val=Math.min(val,1+dp[0][i-1]);\n            }\n            if(swapA>B[i-1]&&swapB>A[i-1]){\n                val=Math.min(val,1+dp[1][i-1]);\n            }\n            dp[1][i]=val;\n        }\n        return Math.min(dp[0][dp[0].length-1],dp[1][dp[0].length-1]);\n    }\n    \n}\n```
[]