✅ Memoization | With Explanation | Simple | C++
\nclass Solution {\npublic:\n    vector <vector <int>> dp;\n    // dp[i][0] -> swaps needed to make array from index i to n stricly increasing considering index
```\nclass Solution {\npublic:\n    vector <vector <int>> dp;\n    // dp[i][0] -> swaps needed to make array from index i to n stricly increasing considering index i is not swapped\n    // dp[i][1] -> swaps needed to make array from index i to n stricly increasing considering index i is swapped\n    \n    int helper(vector<int> &nums1, vector<int>& nums2, int i, int p1, int p2){\n        \n\t\t// Base Case -> if we reach the end of the array return 0\n        if(i >= nums1.size())\n            return 0;\n        \n        // if answer to the current index for both the states is found, return saved answer\n        if((dp[i][0] != INT_MAX and  dp[i][1] != INT_MAX))\n            return min (dp[i][0], dp[i][1]);\n        \n        int option1 = INT_MAX;\n        // if x1 in nums1 and x2 in nums2 is greater than their previous, there is no need to swap\n        if(p1 < nums1[i] and p2 < nums2[i])\n            option1 = helper(nums1, nums2, i + 1, nums1[i], nums2[i]);\n        \n        // save answer of not swapping\n        dp[i][0] = option1;\n        \n        // swap the current numbers\n        swap(nums1[i], nums2[i]);\n        \n        int option2 = INT_MAX;\n        // now if the current numbers are greater than previous, we add 1 operation and return the rest of operations from index i + 1\n        if(p1 < nums1[i] and p2 < nums2[i]){\n            option2 = 1 + helper(nums1, nums2, i +1 , nums1[i], nums2[i]);\n        //save answer of swapping\n            dp[i][1] = option2;\n        }\n            \n        \n        // unswap the current number for the recursion tree to continue\n        swap(nums1[i], nums2[i]);\n\t\t\n        // also consider a case where  \n        // 1. current numbers are greater than their previous \n        // 2. if we swap the numbers, the current now is also greater than their previous\n        // eg A = [1,7] B = [5,9]\n        // at i = 1 we can afford to swap the current index and also afford to not swap it\n        // therefore the answer will be the minimum of swapping and not swapping\n        \n\n        return min (option1, option2);\n            \n        \n    }\n    \n    \n    int minSwap(vector<int>& nums1, vector<int>& nums2) {\n        \n        dp.resize(nums1.size(), vector <int> (2, INT_MAX));\n        return helper(nums1, nums2, 0, -1, -1);\n    }\n};\n```
[]