Easy Recursive approach, Memoisation, Tabulation, and space Optimised
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:\n\n    int solve(int index, vector<int> &nums1, vector<int> &nums2, bool flag){\n        int n = nums1.size();\n        // base case\n        if(index == n) return 0;\n\n        int ans = INT_MAX;\n        int prev1 = nums1[index-1];\n        int prev2 = nums2[index-1];\n\n        if(flag) // we flag is true then we swap prev1 and prev2\n            swap(prev1,prev2);\n        \n        //noswap\n        if(prev1< nums1[index] && prev2< nums2[index]){\n            ans = solve(index+1,nums1,nums2,0);\n        }\n\n        //swap\n        if(prev1< nums2[index] && prev2< nums1[index]){\n            ans = min(ans, 1+ solve(index+1,nums1,nums2,1));\n        }\n        return ans;\n    }\n\n    int solveMem(int index, vector<int> &nums1, vector<int> &nums2, bool flag, vector<vector<int>> &dp){\n        int n = nums1.size();\n        // base case\n        if(index == n) return 0;\n\n        if(dp[index][flag]!= -1) return dp[index][flag];\n\n        int ans = INT_MAX;\n        int prev1 = nums1[index-1];\n        int prev2 = nums2[index-1];\n\n        if(flag) // we flag is true then we swap prev1 and prev2\n            swap(prev1,prev2);\n        \n        //noswap\n        if(prev1< nums1[index] && prev2< nums2[index]){\n            ans = solveMem(index+1,nums1,nums2,0,dp);\n        }\n\n        //swap\n        if(prev1< nums2[index] && prev2< nums1[index]){\n            ans = min(ans, 1+ solveMem(index+1,nums1,nums2,1,dp));\n        }\n        return dp[index][flag] = 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 flag =1;flag>=0;flag--){\n                int ans = INT_MAX;\n                int prev1 = nums1[index-1];\n                int prev2 = nums2[index-1];\n\n                if(flag) // we flag is true then we swap prev1 and prev2\n                    swap(prev1,prev2);\n                \n                //noswap\n                if(prev1< nums1[index] && prev2< nums2[index]){\n                    ans = dp[index+1][0];\n                }\n\n                //swap\n                if(prev1< nums2[index] && prev2< nums1[index]){\n                    ans = min(ans, 1+ dp[index+1][1]);\n                }\n                dp[index][flag]= ans;\n            }\n        }\n        return dp[1][0];\n    }\n\n    int solveSO(vector<int> &nums1, vector<int> &nums2){\n        int n = nums1.size();\n        vector<int> curr(2,0);\n        vector<int> next(2,0);\n\n        for(int index=n-1;index>=1;index--){\n            for(int flag =1;flag>=0;flag--){\n                int ans = INT_MAX;\n                int prev1 = nums1[index-1];\n                int prev2 = nums2[index-1];\n\n                if(flag) // we flag is true then we swap prev1 and prev2\n                    swap(prev1,prev2);\n                \n                //noswap\n                if(prev1< nums1[index] && prev2< nums2[index]){\n                    ans = next[0];\n                }\n\n                //swap\n                if(prev1< nums2[index] && prev2< nums1[index]){\n                    ans = min(ans, 1+ next[1]);\n                }\n                curr[flag]= ans;\n            }\n            next = curr;\n        }\n        return next[0];\n    }\n\n    int minSwap(vector<int>& nums1, vector<int>& nums2) {\n        int n = nums1.size();\n        nums1.insert(nums1.begin(),-1);\n        nums2.insert(nums2.begin(),-1);\n\n        bool flag = 0;\n        // return solve(1,nums1,nums2,flag);\n\n        // vector<vector<int>>  dp(n+1, vector<int>(2,-1));\n        // return solveMem(1,nums1,nums2,flag,dp);\n\n\n        // return solveTab(nums1,nums2);\n\n        return solveSO(nums1,nums2);\n    }\n};\n```
['C++']