[C++] Memoization -> Space Optimization, O(1) Space, clean code
1. Memoization\n\n\nclass Solution {\nprivate:\n    int solve(vector<int> &nums1, vector<int> &nums2, int index, int swapped, vector<vector<int> > &dp){\n      
***1. Memoization***\n\n```\nclass Solution {\nprivate:\n    int solve(vector<int> &nums1, vector<int> &nums2, int index, int swapped, vector<vector<int> > &dp){\n        if(index >= nums1.size()) return 0;\n        \n        if(dp[index][swapped]!=-1) return dp[index][swapped];\n        \n        int prev1 = nums1[index-1];\n        int prev2 = nums2[index-1];\n        int ans = INT_MAX;\n        //if elements at last index were swapped then we need to swap our prev1 and prev2, logically.\n        if(swapped) swap(prev1, prev2);\n        \n        //check if swap needed for elements at \'index\'.\n        if(nums1[index] > prev1 && nums2[index] > prev2){\n            //no need of swap\n            ans = min(ans, solve(nums1, nums2, index+1, 0, dp)) ;\n        }\n        if(nums1[index]>prev2 && nums2[index]>prev1){\n            //swap, count +1;\n            ans = min(ans, 1+solve(nums1, nums2, index+1, 1, dp));\n        }\n        \n        return dp[index][swapped] = ans;\n    }\npublic:\n    int minSwap(vector<int>& nums1, vector<int>& nums2) {\n        //we put some -1 at the start of each array to make comparison easy.\n        nums1.insert(nums1.begin(), -1);\n        nums2.insert(nums2.begin(), -1);\n        \n        //this variable will show if the last index was swapped or not(if swapped 1, else 0);\n        int swapped = 0, index = 1;\n        vector<vector<int> > dp(nums1.size(), vector<int>(2, -1));\n        return solve(nums1, nums2, index, swapped, dp);\n    }\n};\n```\n\n***2. Tabulation***\n\n```\nclass Solution {\npublic:\n    int minSwap(vector<int>& nums1, vector<int>& nums2) {\n        //we put some -1 at the start of each array to make comparison easy.\n        int n = nums1.size();\n        nums1.insert(nums1.begin(), -1);\n        nums2.insert(nums2.begin(), -1);\n\n        vector<vector<int> > dp(n+2, vector<int>(2, 0));\n        \n        for(int index = n; index >= 1; index--){\n            for(int swapped = 0; swapped < 2; swapped++){\n                int prev1 = nums1[index-1];\n                int prev2 = nums2[index-1];\n                int ans = INT_MAX;\n                //if elements at last index were swapped then we need to swap our prev1 and prev2, logically.\n                if(swapped) swap(prev1, prev2);\n\n                //check if swap needed for elements at \'index\'.\n                if(nums1[index] > prev1 && nums2[index] > prev2){\n                    //no need of swap\n                    ans = min(ans, dp[index+1][0]) ;\n                }\n                if(nums1[index]>prev2 && nums2[index]>prev1){\n                    //swap, count +1;\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```\n\n***3. Space Optimization, Constant space***\n\n```\nclass Solution {\npublic:\n    int minSwap(vector<int>& nums1, vector<int>& nums2) {\n        int n = nums1.size(), prev1, prev2, ans;\n        \n        //we put some -1 at the start of each array to make comparison easy.\n        nums1.insert(nums1.begin(), -1);\n        nums2.insert(nums2.begin(), -1);\n        \n        vector<int> next(2, 0), cur(2, 0);\n        \n        for(int index = n; index >= 1; index--){\n            for(int swapped = 0; swapped < 2; swapped++){\n                prev1 = nums1[index-1];\n                prev2 = nums2[index-1];\n                ans = INT_MAX;\n                //if elements at last index were swapped then we need to swap our prev1 and prev2, logically.\n                if(swapped) swap(prev1, prev2);\n\n                //check if swap needed for elements at \'index\'.\n                if(nums1[index] > prev1 && nums2[index] > prev2){\n                    //no need of swap\n                    ans = min(ans, next[0]) ;\n                }\n                if(nums1[index]>prev2 && nums2[index]>prev1){\n                    //swap, count +1;\n                    ans = min(ans, 1 + next[1]);\n                }\n                cur[swapped] = ans;\n            }\n            next = cur;\n        }\n        return next[0];\n    }\n};\n```\n\n*Comment below for any queries.\nUpvote if it helps.*
['Dynamic Programming', 'Recursion', 'Memoization', 'C', 'C++']