✅✅Faster || Easy To Understand || C++ Code
Approach 1 :- Using Recursion && Memoization\n\n Time Complexity :- O(N)\n\n Space Complexity :- O(N)\n\n\nclass Solution {\npublic:\n    \n    int dp[100005][2
* ***Approach 1 :- Using Recursion && Memoization***\n\n* ***Time Complexity :- O(N)***\n\n* ***Space Complexity :- O(N)***\n\n```\nclass Solution {\npublic:\n    \n    int dp[100005][2];\n    \n    int dfs(vector<int>& nums1, vector<int>& nums2, int i, int n, int prev1, int prev2, bool swap)\n    {\n        if(i == n)\n            return 0;\n        \n        // if already calculated\n        \n        if(dp[i][swap] != -1)\n            return dp[i][swap];\n        \n        int ans = INT_MAX;\n        \n        // no swaps have been made\n        \n        if(nums1[i] > prev1 && nums2[i] > prev2)\n        {\n            ans = min(ans, dfs(nums1, nums2, i + 1, n, nums1[i], nums2[i], false));\n        }\n        \n        // swaps have been made\n        \n        if(nums1[i] > prev2 && nums2[i] > prev1)\n        {\n            ans = min(ans, 1 + dfs(nums1, nums2, i + 1, n, nums2[i], nums1[i], true));\n        }\n        \n        // store in dp\n        \n        return dp[i][swap] = ans;\n    }\n    \n    int minSwap(vector<int>& nums1, vector<int>& nums2) {\n        \n        int n = nums1.size();\n        \n        memset(dp, -1, sizeof(dp));\n        \n        return dfs(nums1, nums2, 0, n, -1, -1, false);\n    }\n};\n```
['Dynamic Programming', 'Recursion', 'Memoization', 'C']