Java DP 2ms solution with comments
\npublic int minSwap(int[] A, int[] B) {\n        int n = A.length;\n        int not_swap[] = new int[n]; \n        //not_swap[i] -> min swaps to make {A[0]~A[i
```\npublic int minSwap(int[] A, int[] B) {\n        int n = A.length;\n        int not_swap[] = new int[n]; \n        //not_swap[i] -> min swaps to make {A[0]~A[i]} and {B[0]~B[i]} without swapping A[i] and B[i]\n        int swap[] = new int[n];\n        //swap[i] -> min swaps to make {A[0]~A[i]} and {B[0]~B[i]} with swapping A[i] and B[i]\n        Arrays.fill(not_swap, Integer.MAX_VALUE);\n        Arrays.fill(swap, Integer.MAX_VALUE);\n        not_swap[0] = 0;\n        swap[0] = 1;\n        \n        for(int i = 1; i < n; i++) {\n            if(A[i - 1] < A[i] && B[i - 1] < B[i]) {\n                swap[i] = swap[i - 1] + 1; //swap both A[i - 1], B[i - 1] & A[i], B[i]\n                not_swap[i] = not_swap[i - 1]; //don\'t swap both A[i - 1], B[i - 1] & A[i], B[i]\n            }\n            if(A[i] > B[i - 1] && B[i] > A[i - 1]) {\n                swap[i] = Math.min(swap[i], not_swap[i - 1] + 1); //if we swap A[i],B[i], we don\'t need to swap A[i - 1],B[i - 1] \n                //not_swap[i - 1] + 1 because we didn\'t swap A[i - 1] & B[i - 1] and +1 for current swap \n                not_swap[i] = Math.min(not_swap[i], swap[i - 1]); //if we swap A[i - 1],B[i - 1], we don\'t need to swap A[i],B[i]\n            }\n        }\n        \n        return Math.min(swap[n - 1], not_swap[n - 1]);\n    }\n```
[]