Recursive -> TopDown -> BottomUP -> Space Optimized
\nclass Solution {\nprivate:\n    int recursive(vector<int>& nums1, vector<int>& nums2, int idx, int swapped){\n        cout << idx << " " << swapped << endl;\n
```\nclass Solution {\nprivate:\n    int recursive(vector<int>& nums1, vector<int>& nums2, int idx, int swapped){\n        cout << idx << " " << swapped << endl;\n        if(idx == nums1.size()) return 0;\n        int ans = INT_MAX;\n        if(idx == 0 || nums1[idx] > nums1[idx - 1] && nums2[idx] > nums2[idx - 1]){\n            ans =  recursive(nums1, nums2, idx + 1, 0);\n        }\n        if(idx == 0 || nums1[idx] > nums2[idx - 1] && nums2[idx] > nums1[idx - 1]){\n            swap(nums1[idx], nums2[idx]);\n            ans = min(ans, 1 + recursive(nums1, nums2, idx + 1, 1));\n            swap(nums1[idx], nums2[idx]);\n        }\n        return ans;\n    }\n    int topDown(vector<int>& nums1, vector<int>& nums2, int idx, int swapped, vector<vector<int>> &dp){\n        if(idx == nums1.size()) return 0;\n        if(dp[idx][swapped] != -1) return dp[idx][swapped];\n        int ans = INT_MAX;\n        if(idx == 0 || nums1[idx] > nums1[idx - 1] && nums2[idx] > nums2[idx - 1]){\n            ans =  topDown(nums1, nums2, idx + 1, 0, dp);\n        }\n        if(idx == 0 || nums1[idx] > nums2[idx - 1] && nums2[idx] > nums1[idx - 1]){\n            swap(nums1[idx], nums2[idx]);\n            ans = min(ans, 1 + topDown(nums1, nums2, idx + 1, 1, dp));\n            swap(nums1[idx], nums2[idx]);\n        }\n        return dp[idx][swapped] = ans;\n    }\n    int bottomUp(vector<int>& nums1, vector<int>& nums2){\n        vector<vector<int>> dp(nums1.size() + 1, vector<int>(2, 0));\n        for(int idx = nums1.size() - 1 ; idx >= 0 ; idx--){\n            for(int swapped = 1; swapped >= 0; swapped--){\n                int ans = INT_MAX;\n                if(swapped){\n                    swap(nums1[idx], nums2[idx]);\n                }\n                if(idx == 0 || nums1[idx] > nums1[idx - 1] && nums2[idx] > nums2[idx - 1]){\n                    ans =  dp[idx + 1][0];\n                }\n                if(idx == 0 || nums1[idx] > nums2[idx - 1] && nums2[idx] > nums1[idx - 1]){\n                    ans = min(ans, 1 + dp[idx + 1][1]);\n                }\n                if(swapped){\n                    swap(nums1[idx], nums2[idx]);\n\n                }\n                dp[idx][swapped] = ans;   \n            }\n        }\n        return dp[0][0];\n    }\n\n    int spaceOptimized(vector<int>& nums1, vector<int>& nums2){\n        vector<int> dp(2, 0);\n        for(int idx = nums1.size() - 1 ; idx >= 0 ; idx--){\n            vector<int> temp(2);\n            for(int swapped = 1; swapped >= 0; swapped--){\n                int ans = INT_MAX;\n                if(swapped){\n                    swap(nums1[idx], nums2[idx]);\n                }\n                if(idx == 0 || nums1[idx] > nums1[idx - 1] && nums2[idx] > nums2[idx - 1]){\n                    ans =  dp[0];\n                }\n                if(idx == 0 || nums1[idx] > nums2[idx - 1] && nums2[idx] > nums1[idx - 1]){\n                    ans = min(ans, 1 + dp[1]);\n                }\n                if(swapped){\n                    swap(nums1[idx], nums2[idx]);\n\n                }\n                temp[swapped] = ans;   \n            }\n            dp = temp;\n        }\n        return dp[0];\n    }\n\npublic:\n    int minSwap(vector<int>& nums1, vector<int>& nums2) {\n\n        int swapped = 0;\n        // return recursive(nums1, nums2, 1, swapped);\n        \n        //topDown\n        // vector<vector<int>> dp(nums1.size(), vector<int>(2, -1));\n        // return topDown(nums1, nums2, 0, swapped, dp);\n        \n        //Bottom Up\n        // return bottomUp(nums1, nums2);\n        return spaceOptimized(nums1, nums2);\n\n    }\n};\n```
['Dynamic Programming', 'Recursion']