recursion -> memoization
Recursion (TLE)\n\nclass Solution {\npublic:\n    int solve(vector<int> num1,vector<int> num2,int i,int prev1,int prev2){\n        int n = num1.size();\n       
**Recursion (TLE)**\n```\nclass Solution {\npublic:\n    int solve(vector<int> num1,vector<int> num2,int i,int prev1,int prev2){\n        int n = num1.size();\n        if(i == n){\n            return 0;\n        }\n        \n        int noSwap=1e9;\n        if(num1[i]>prev1 && num2[i]>prev2){\n            noSwap = solve(num1,num2,i+1,num1[i],num2[i]);\n        }\n        \n        int swap=1e9;\n        if(num1[i]>prev2 && num2[i]>prev1){\n            swap = 1 + solve(num1,num2,i+1,num2[i],num1[i]);\n        }\n        \n        return min(swap,noSwap);\n    }\n    int minSwap(vector<int>& nums1, vector<int>& nums2) {\n        int n=nums1.size();\n        int m=nums2.size();\n        \n        return solve(nums1,nums2,0,-1,-1);\n    }\n};\n```\n**Memoization**\n```\nclass Solution {\npublic:\n    vector<vector<int>> dp;\n    \n    int solve(vector<int> &num1,vector<int> &num2,int i,int prev1,int prev2,bool swaped){\n        int n = num1.size();\n        if(i == n){\n            return 0;\n        }\n        \n        if(dp[i][swaped] != -1){\n            return dp[i][swaped];\n        }\n        \n        int noSwap=1e9;\n        if(num1[i]>prev1 && num2[i]>prev2){\n            noSwap = solve(num1,num2,i+1,num1[i],num2[i],false);\n        }\n        \n        int swap=1e9;\n        if(num1[i]>prev2 && num2[i]>prev1){\n            swap = 1 + solve(num1,num2,i+1,num2[i],num1[i],true);\n        }\n        \n        return dp[i][swaped] = min(swap,noSwap);\n    }\n    \n    \n    int minSwap(vector<int>& nums1, vector<int>& nums2) {\n        int n=nums1.size();\n        dp.resize(n+1,vector<int> (2,-1));\n        \n        return solve(nums1,nums2,0,-1,-1,0);\n    }\n};\n```
['Dynamic Programming', 'Recursion', 'Memoization', 'C']