solution using recursion and memoization
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\nint solve(int i,int p,vector<int> &nums1,vector<int> &nums2,vector<vector<int>> &dp){\n    int n=nums1.size();\n    if(i==n) return 0;\n    if(dp[i][p]!=-1) return dp[i][p];\n    int p1,p2;\n    if(i>0&&p==0) {\n        p1=nums1[i-1];\n        p2=nums2[i-1];\n    }\n    else if(i>0&&p==1){\n        p1=nums2[i-1];\n        p2=nums1[i-1];\n    }\n    if(i!=0){\n    if((nums1[i]<=p1||nums2[i]<=p2)&&(nums1[i]<=p2||nums2[i]<=p1)) return INT_MAX-1;\n    }\n    int sp=INT_MAX;\n    int np=INT_MAX;\n    if(i==0){\n      sp=1+solve(i+1,1,nums1,nums2,dp);\n      np=solve(i+1,0,nums1,nums2,dp);\n    }\n    else{\n        if((nums1[i]>p1&&nums2[i]>p2)&&(nums1[i]>p2&&nums2[i]>p1)) {\n             sp=1+solve(i+1,1,nums1,nums2,dp);\n             np=solve(i+1,0,nums1,nums2,dp);\n        }\n        else if(nums1[i]<=p1||nums2[i]<=p2)\n        sp=1+solve(i+1,1,nums1,nums2,dp);\n        else\n        np=solve(i+1,0,nums1,nums2,dp);\n    }\n    return dp[i][p]= min(sp,np);\n}\n    int minSwap(vector<int>& nums1, vector<int>& nums2) {\n         int n=nums1.size();\n         vector<vector<int>> dp(n+1,vector<int>(2,-1));\n         return solve(0,0,nums1,nums2,dp);\n    }\n};\n```
['C++']