[Java] DP Iterative solution
DP Iterative solution\nFor each element in both arrays we should maintain the invariant:\nnums[i - 1] < nums[i]\n\nStates:\ndp[i][0] minimum number of swaps to 
**DP Iterative solution**\nFor each element in both arrays we should maintain the invariant:\n`nums[i - 1] < nums[i]`\n\nStates:\n`dp[i][0]` minimum number of swaps to have [0:i] sorted and no swap made at i index\n`dp[i][1] ` minimum number of swaps to have [0:i] sorted and swap i index of both arrays\n\nFor each element we make sure that nums[i - 1] < nums[i]\nThere are 4 cases:\n1. i - 1 elements not swapped, i elements not swapped. Because no swaps happened array values at index i - 1 and i remain the same. And we update not swapped state.\n```\nif(nums1[i - 1] < nums1[i] && nums2[i - 1] < nums2[i]) {\n\tdp[i][0] = dp[i - 1][0];              \n}\n```\nCompare this case with the remaining 3 keeping into account changes in values at i - 1 and i indices.\n\n2. i - 1 elements swapped, i element not swapped. Because i - 1 element swapped, array values at i - 1 changed: nums1[i - 1] = nums2[i - 1], and nums2[i - 1] = nums1[i - 1] so we do similar check, but with that difference. And we update not swapped state and read previous swapped state.\n```\nif(nums2[i - 1] < nums1[i] && nums1[i - 1] < nums2[i]) {\n\tdp[i][0] = Math.min(dp[i][0], dp[i - 1][1]);\n}\n```\n3. i - 1 elements not swapped, i elements swapped. Because i index is swapped, array values at index i changed so that nums1[i] = nums2[i] and nums2[i] = nums1[i]. So we should take that into account when comparing. And we update swapped state with the previous not swapped value.\n```\nif(nums1[i - 1] < nums2[i] && nums2[i - 1] < nums1[i]) {\n\tdp[i][1] = dp[i - 1][0] + 1;            \n}\n```\n4. i - 1 elements swapped, i elements swapped. Because array values at both i - 1 and i indices changed that means both i - 1 and i indices of nums1 array now have values from nums2 array. Similarly both i - 1 and i indices of nums2 array now have values from nums1 array. And we update swapped state with the previous swapped state.\n```\nif(nums2[i - 1] < nums2[i] && nums1[i - 1] < nums1[i]) {\n\tdp[i][1] = Math.min(dp[i][1], dp[i - 1][1] + 1);\n}\n```\n\n**Code:**\n```\nclass Solution {\n    final static int INF = (int) 1e9 + 7;\n    public int minSwap(int[] nums1, int[] nums2) {\n        int n = nums1.length;\n        int[][] dp = new int[n][2];\n        dp[0][0] = 0;\n        dp[0][1] = 1;        \n        for(int i = 1; i < n; i++) {\n            dp[i][0] = dp[i][1] = INF;\n            //last time no swap and this time no swap\n            if(nums1[i - 1] < nums1[i] && nums2[i - 1] < nums2[i]) {\n                dp[i][0] = dp[i - 1][0];              \n            }\n            //last time swap this time no swap\n            if(nums2[i - 1] < nums1[i] && nums1[i - 1] < nums2[i]) {\n                dp[i][0] = Math.min(dp[i][0], dp[i - 1][1]);\n            }\n            //last time no swap this time swap\n            if(nums1[i - 1] < nums2[i] && nums2[i - 1] < nums1[i]) {\n                dp[i][1] = dp[i - 1][0] + 1;            \n            }\n            //last time swap this time swap\n            if(nums2[i - 1] < nums2[i] && nums1[i - 1] < nums1[i]) {\n                dp[i][1] = Math.min(dp[i][1], dp[i - 1][1] + 1);\n            }            \n        }\n        \n        return Math.min(dp[n - 1][0], dp[n - 1][1]);\n    }\n}\n```
['Dynamic Programming', 'Iterator', 'Java']