All approach ...Recursion To.......DP............/\/\/\/\....................
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```cpp []\nclass Solution {\npublic:\n    int solve(vector<int>& nums1, vector<int>& nums2,int index,bool swapped){\n        if(index==nums1.size()){\n            return 0;\n        }\n\n        int prev1=nums1[index-1];\n        int prev2=nums2[index-1];\n        int ans=INT_MAX;\n\n        if(swapped){\n            swap(prev1,prev2);\n        }\n\n        if(nums1[index]>prev1&&nums2[index]>prev2){\n            ans=solve(nums1,nums2,index+1,0);\n        }\n\n        if(nums1[index]>prev2&&nums2[index]>prev1){\n            ans=min(ans,1+solve(nums1,nums2,index+1,1));\n        }\n\n        return ans;\n    }\n\n    int solveMem(vector<int>& nums1, vector<int>& nums2,int index,bool swapped,vector<vector<int>>&dp){\n        if(index==nums1.size()){\n            return 0;\n        }\n\n        if(dp[index][swapped]!=-1){\n            return dp[index][swapped];\n        }\n        int prev1=nums1[index-1];\n        int prev2=nums2[index-1];\n        int ans=INT_MAX;\n\n        if(swapped){\n            swap(prev1,prev2);\n        }\n\n        if(nums1[index]>prev1&&nums2[index]>prev2){\n            ans=solveMem(nums1,nums2,index+1,0,dp);\n        }\n\n        if(nums1[index]>prev2&&nums2[index]>prev1){\n            ans=min(ans,1+solveMem(nums1,nums2,index+1,1,dp));\n        }\n\n        return dp[index][swapped]=ans;\n    }\n\n    int solveTab(vector<int>& nums1, vector<int>& nums2){\n        int n=nums1.size();\n        vector<vector<int>>dp(n+1,vector<int>(2,0));\n\n        for(int index=n-1;index>=1;index--){\n            for(int swapped=1;swapped>=0;swapped--){\n                int prev1=nums1[index-1];\n                int prev2=nums2[index-1];\n                int ans=INT_MAX;\n\n                if(swapped){\n                    swap(prev1,prev2);\n                }\n\n                if(nums1[index]>prev1&&nums2[index]>prev2){\n                    ans=dp[index+1][0];\n                }\n\n                if(nums1[index]>prev2&&nums2[index]>prev1){\n                    ans=min(ans,1+dp[index+1][1]);\n                }\n\n                dp[index][swapped]=ans;\n            }\n        }\n        return dp[1][0];\n    }\n\n    int solveSOP(vector<int>& nums1, vector<int>& nums2){\n        int n=nums1.size();\n        \n        int swap=0;\n        int noswap=0;\n        int currswap=0;\n        int currnoswap=0;\n\n        for(int index=n-1;index>=1;index--){\n            for(int swapped=1;swapped>=0;swapped--){\n                int prev1=nums1[index-1];\n                int prev2=nums2[index-1];\n                int ans=INT_MAX;\n\n                if(swapped){\n                    int temp=prev1;\n                    prev1=prev2;\n                    prev2=temp;\n                    // swap(prev1,prev2);\n                }\n\n                if(nums1[index]>prev1&&nums2[index]>prev2){\n                    ans=noswap;\n                }\n\n                if(nums1[index]>prev2&&nums2[index]>prev1){\n                    ans=min(ans,1+swap);\n                }\n\n                if(swapped)\n                currswap=ans;\n\n                else\n                currnoswap=ans;\n            }\n            swap=currswap;\n            noswap=currnoswap;\n        }\n        return min(swap,noswap);\n    }\n\n    int minSwap(vector<int>& nums1, vector<int>& nums2) {\n        nums1.insert(nums1.begin(),-1);\n        nums2.insert(nums2.begin(),-1);\n        bool swapped=0;\n        int n=nums1.size();\n\n        // return solve(nums1,nums2,1,swapped);\n\n        // vector<vector<int>>dp(n,vector<int>(2,-1));\n        // return solveMem(nums1,nums2,1,swapped,dp);\n\n        // return solveTab(nums1,nums2);\n\n        return solveSOP(nums1,nums2);\n    }\n};\n```
['C++']