JAVA: EASY DP
The idea is very simple, enumerate all cases.\ndp[i][0] do not swap current pair \ndp[i][1] swap current pair\n\n* a1\nif (a1<b2 && b1<a2){\n    dp[i][0] = Math
The idea is very simple, enumerate all cases.\ndp[i][0] do not swap current pair \ndp[i][1] swap current pair\n\n* a1<a2 && b1<b2\n\n```\nif (a1<b2 && b1<a2){\n    dp[i][0] = Math.min(Math.min(dp[i-1][0], dp[i-1][1]), dp[i][0]);\n    dp[i][1] = Math.min(Math.min(dp[i-1][0]+1,dp[i-1][1]+1), dp[i][1]);\n}else{\n    dp[i][0] = Math.min(dp[i-1][0], dp[i][0]);\n    dp[i][1] = Math.min(dp[i-1][1]+1, dp[i][1]);\n}\n```\n\n* a1<a2 && b1>b2\n\n```\nif (a1<b2 && b1<a2)\n\tdp[i][0] = min(dp[i-1][1], dp[i][0])\n\tdp[i][1] = min(dp[i-1][0]+1, dp[i][1])\n```\n\n* a1>a2 && b1<b2\n```\nif (a1<b2 && b1<a2)\n\tdp[i][0] = min(dp[i-1][1], dp[i][0])\n\tdp[i][1] = min(dp[i-1][0]+1, dp[i][1])\n```\n\n* a1>a2 && b1>b2\n```\nif (a1<b2 && b1<a2)\n\tdp[i][0] = min(dp[i-1][1], dp[i][0])\n\tdp[i][1] = min(dp[i-1][0]+1, dp[i][1])\n```\nSo we can simplify \n```\nclass Solution {\n    public int minSwap(int[] nums1, int[] nums2) {\n        int[][] dp = new int[nums1.length][2];\n        //dp[0][0] = 0; if we do not swap, then nb of swap is 0\n        dp[0][1] = 1; // if we swap the first, then nb of swap is 1 \n        for (int i=1; i<nums1.length; i++){\n            int a1 = nums1[i-1];\n            int a2 = nums1[i];\n            int b1 = nums2[i-1];\n            int b2 = nums2[i];\n            dp[i][0] = Integer.MAX_VALUE;\n            dp[i][1] = Integer.MAX_VALUE;\n            if (a1<a2 && b1 <b2){\n                if (a1<b2 && b1<a2){\n                    dp[i][0] = Math.min(Math.min(dp[i-1][0], dp[i-1][1]), dp[i][0]);\n                    dp[i][1] = Math.min(Math.min(dp[i-1][0]+1,dp[i-1][1]+1), dp[i][1]);\n                }else{\n                    dp[i][0] = Math.min(dp[i-1][0], dp[i][0]);\n                    dp[i][1] = Math.min(dp[i-1][1]+1, dp[i][1]);\n                }\n            }else{\n                dp[i][0] = Math.min(dp[i-1][1], dp[i][0]);\n                dp[i][1] = Math.min(dp[i-1][0]+1, dp[i][1]);\n            }\n           // System.out.print(dp[i][0]+","+dp[i][1]+" ");\n        }\n        //System.out.println();\n        return Math.min(dp[nums1.length-1][0], dp[nums1.length-1][1]);\n    }\n}\n```
['Dynamic Programming', 'Java']