Java explanation using DP
The idea is that you have an array int[][] dp = new int[A.length][2], where dp[i][0] means we have the min number of swaps up to index i assuming we didnt swap 
The idea is that you have an array int[][] dp = new int[A.length][2], where dp[i][0] means we have the min number of swaps up to index i assuming we didnt swap the ith column entries, and dp[i][1] means we have the min number of swaps up to index i assuming we swapped the ith column entries.\n\nTo get the next column entries there are four cases:\n\n1.) we swapped column i and swapped column i+1\n2.) we didnt swap column i and didnt swap column i+1\n3.) we swapped column i but not swap i+1\n4.) we didnt swap column i but did swap column i+1\n\nThe first two cases can be encapsulated by the conditional:\n\n```\nif(A[i-1] < A[i] && B[i-1] < B[i]){\n\t\t// this means A[i-1] and A[i] can either be in array A, or array B.\n\t\t// likewise for B[i-1] and B[i]\n}\n```\n\nThus in the above conditional we check the cases for dp[i][0] and dp[i][1], corresponding to case 2 and 1 respectively. For cases 3 and 4, we use the conditional:\n\n```\nif(A[i-1] < B[i] && B[i-1] < A[i]){\n\t\t// this means A[i-1] and B[i] can either be in array A, or array B.\n\t\t// likewise for B[i-1] and A[i]\n}\n```\n\nWhere we check for case dp[i][0] and dp[i][1]. We take the mins for each one.\n\nThe final code is as follows:\n\n```\nclass Solution {\n    public int minSwap(int[] A, int[] B) {\n        \n        int[][] dp = new int[A.length][2];\n        dp[0][0] = 0; \n        dp[0][1] = 1; \n\n        for(int i=1; i<A.length; i++){\n            \n            dp[i][0] = Integer.MAX_VALUE;\n            dp[i][1] = Integer.MAX_VALUE;\n            \n            if(A[i-1] < A[i] && B[i-1] < B[i]){\n                dp[i][0] = Math.min(dp[i][0],dp[i-1][0]); //dont swap i and dont swap i-1\n                dp[i][1] = Math.min(dp[i][1],dp[i-1][1]+1); //swap i and i-1\n            }\n            \n            if(A[i-1] < B[i] && B[i-1] < A[i]){\n                dp[i][1] = Math.min(dp[i][1],dp[i-1][0]+1); //dont swap i-1 and swap i\n                dp[i][0] = Math.min(dp[i][0],dp[i-1][1]); // dont swap i and swap i-1\n            }\n            \n        }\n\n        return Math.min(dp[dp.length-1][0],dp[dp.length-1][1]);        \n    }\n}\n```\n\nA final exercise to the reader is converting the above DP approach to constant space (this is how you get the answer provided as an explanation).
[]