Java | 1D DP | easy-understanding
There are only 2 cases for each pair of numbers; otherwise, the input is invalid.\n    (a) nums1[i] > nums1[i - 1] && nums2[i] > nums2[i - 1]\n\t(b) nums1[i] > 
1. There are only 2 cases for each pair of numbers; otherwise, the input is invalid.\n    (a) nums1[i] > nums1[i - 1] && nums2[i] > nums2[i - 1]\n\t(b) nums1[i] > nums2[i - 1] && nums2[i] > nums1[i - 1]\n2. For each pair of numbers in the arrays, there are 2 states: swap or no swap\n3. State transition for Case (a)\n    \t\t\tnoSwap[i] = noSwap[i - 1];\n    \t\t\tswap[i] = swap[i - 1] + 1;\n4. State transition for Case (b)\n    \t\t\tnoSwap[i] = swap[i - 1];\n    \t\t\tswap[i] = noSwap[i - 1] + 1;\n```\n    public int minSwap(int[] nums1, int[] nums2) {\n    \tint n = nums1.length;\n    \tint[] swap = new int[n], noSwap = new int[n];\n        swap[0] = nums1[0] == nums2[0] ? 0 : 1;\n    \tfor (int i = 1; i < n; i++) {\n    \t\tnoSwap[i] = swap[i] = n;  // the value is initialized because Math.min( ) is used in the following codes\n    \t\tif (nums1[i] > nums1[i - 1] && nums2[i] > nums2[i - 1]) {\n    \t\t\tnoSwap[i] = noSwap[i - 1];\n    \t\t\tswap[i] = swap[i - 1] + 1;\n    \t\t} \n    \t\tif (nums1[i] > nums2[i - 1] && nums2[i] > nums1[i - 1]) {\n    \t\t\tnoSwap[i] = Math.min(noSwap[i], swap[i - 1]);\n    \t\t\tswap[i] = Math.min(swap[i], noSwap[i - 1] + 1);\n    \t\t}\n    \t}\n    \treturn Math.min(swap[n - 1], noSwap[n - 1]);\n    }\n\n
['Dynamic Programming', 'Java']