Simple DP in Java  with time:O(N) space:O(1) 
\nIf you use the recursive function, the recursion would be something like:\n\nO swapped       X not swapped\n\n[0]              O                  X\n[1]      
```\nIf you use the recursive function, the recursion would be something like:\n\nO swapped       X not swapped\n\n[0]              O                  X\n[1]           O     X            O     X\n[2]         O  X   O  X        O  X   O  X  \n :\n \n \nOptimal Subtructure would be:\n \n [i]             O + 1\n            min(/ \\)\n [i+1]         O   X\n\n [i]             X + 0\n            min(/ \\)\n [i+1]         O   X\n \nAll possible conditions are shown below: \n\n[i]  -->              (Not Swapped)  A   B          \n[i+1] -->  (Swapped) B  A                     (Not Swapped) A  B\n\n[i] -->                   (Swapped)  B   A          \n[i+1] -->  (Swapped)  B  A                    (Not Swapped) A  B\n\n```\nThe following is the code.\n```\npublic int minSwap(int[] A, int[] B) {\n    int l = A.length;\n    int[] pre = new int[]{0, 1};\n    for(int i = l - 2; i >= 0; i--) {\n        int[] cur = new int[]{0, 1};\n        /* dp[0] --> not swap current */\n        int min = Integer.MAX_VALUE;\n        if(A[i] < A[i + 1] && B[i] < B[i + 1]) min = Math.min(min, pre[0]);\n        if(A[i] < B[i + 1] && B[i] < A[i + 1]) min = Math.min(min, pre[1]); \n        cur[0] += min;\n\n        /* dp[1] --> swap current */\n        min = Integer.MAX_VALUE;\n        if(B[i] < A[i + 1] && A[i] < B[i + 1]) min = Math.min(min, pre[0]);\n        if(B[i] < B[i + 1] && A[i] < A[i + 1]) min = Math.min(min, pre[1]);\n        cur[1] += min;\n\n        /* update pre */\n        pre = cur;\n    }\n    return Math.min(pre[0], pre[1]);\n}\n```
[]