FULLY EXPLAINED SOLUTION FOR BEGINNER . ( Recursion+Memoization)
The basic idea behind this problem is same what we uses in 0 1 knapsack.\nSo what is the idea in 0 1 knapsack. The idea is that we provide some condition during
The basic idea behind this problem is same what we uses in 0 1 knapsack.\nSo what is the idea in **0 1 knapsack**. The idea is that we provide some condition during function call, and if condition is satisfied then we have two options (i,e) either include the current element or do not include the current element.But if condition is not satisfied then we have only one option (i,e) we do not include the current element.\n\nWe will use that same idea here.\nSo what are the different conditions here.\n\nCondition 1 - Ok lets suppose we have two arrays as follows :-\n\tnums1[] = [3,2]\n\tnums2[] = [1,4]\n\tand we are at index 1.we can see that for nums1 we have nums1[ind-1]>= nums1[ind], So for this condition we have only one option i,e we have to swap the elements so that array can be made strictly increasing. So we will swap elements and recursively call next functions.\n\t\n\tAfter swapping element at index 1 array will now becomes nums1[] = {3,4} and nums2[] = {1,2} (i,e strictly increasing)\n\t\nCondition 2- lets take another example:-\nnums1[] = {1,2}\nnums2[] = {3,4}\nindex = 1;\n\nif we do swapping of the current element then our array will be nums1[] = {1,4} and nums2[] = {3,2}. We can see that the elements in nums2 is not in increasing order.\n\nTherefore if (nums1[ind-1] >= nums2[ind] || nums2[ind-1] >= nums1[ind] ) we cannot swap the current element.\n\nCondition 3 -\nNow lets take another example \nnums1[] = {1,5}\nnums2[] = {3,4}\nindex = 1;\n\nHere we have two options either we swap the current element or we do not swap. So here we will take the minimum answer of both the option and return the answer.\n\nAny suggestions are welcome.\nThanks\n**Do upvote if you like the explaination !!!**\n\nclass Solution {\n\n    public int minSwap(int[] nums1, int[] nums2) {\n\t\n        int dp[] = new int[nums1.length];\n        Arrays.fill(dp,-1);\n        \n        int ans = solve(nums1,nums2,0,dp);\n        \n        return ans;\n    }\n    \n    public int solve(int nums1[],int nums2[],int ind,int[] dp){\n        \n        if(ind == nums1.length) return 0;\n        \n\t\t// Condition 1\n\t\t\n        if(ind>0 && (nums1[ind-1]>=nums1[ind] || nums2[ind-1]>=nums2[ind])) {\n            \n\t\t\t\t//\tnums1[] = [3,2]\n\t\t\t\t//\tnums2[] = [1,4]\n\t\t\t\t\n                int t = nums1[ind];\n                nums1[ind] = nums2[ind];\n                nums2[ind] = t;\n                \n               int val = 1+solve(nums1,nums2,ind+1,dp);\n            \n\t\t\t// Since after swapping array becomes \n\t\t\t// nums1[] = [3,4]\n\t\t\t// nums2[] = [1,2]\n\t\t\t// therefore we have to swap it back so that we can have our original array.\n\t\t\t\n                t = nums1[ind];\n                nums1[ind] = nums2[ind];\n                nums2[ind] = t;\n            \n            return val;\n            \n        }  \n\t\t// Condition 2\n\t\t\n\t\telse if(ind>0 && (nums1[ind-1]>=nums2[ind] || nums2[ind-1]>=nums1[ind])) {\n\t\t\t\treturn solve(nums1,nums2,ind+1,dp);\n        }\n\t\t// Condition 3\n\t\t\n        else {\n            if(dp[ind] != -1) return dp[ind];\n            \n                int tempAns1 = solve(nums1,nums2,ind+1,dp);\n        \n                int t = nums1[ind];\n                nums1[ind] = nums2[ind];\n                nums2[ind] = t;\n        \n                int tempAns2 = 1+solve(nums1,nums2,ind+1,dp);\n            \n                t = nums1[ind];\n                nums1[ind] = nums2[ind];\n                nums2[ind] = t;\n        \n             return dp[ind] = Math.min(tempAns1,tempAns2);\n        }\n        \n    }\n}\n\nA pictorial representation of why we are updating dp[] array only in condition 3.\n**nums1[] = [4,4,5];\nnums2[] = [1,6,8];**\n![image](https://assets.leetcode.com/users/images/1143fc30-6aae-4421-9819-dfc69e15e5b3_1647628365.1513515.jpeg)\n\n
['Java']