🔥🎯Clean O(n) DP approach🎯🔥
Intuition\nFor every index i we can either swap it if the strictly increasing rule is being broken or keep it as it is.\n\u2234 We have two Choices.\n\n# Approa
# Intuition\nFor every index i we can either swap it if the strictly increasing rule is being broken or keep it as it is.\n\u2234 We have two $$Choices$$.\n\n# Approach\nIf at any index $$i$$ we have the swaps needed for sub array [ $$i+1$$ to $$n$$ ]\nthen we can calculate result for $$i$$ $$th$$ index.\n\n### Case1 : \n**Invalide case**\nCurrent element of nums1 / nums2 is greater or equal to next element.\n\n - We **have** to swap **either** $$i$$ $$th$$ elements **or** $$i+1$$ $$th$$ elements. \n\n    **Note :** if you swap both $$i$$ $$th$$ elements and $$i+1$$ $$th$$ elements you will atain the same Invalid case again.\n\n - So we will add the ( result / cost / number of operations needed ) of $$swapping$$ $$i$$ $$th$$ elements with $$notSwapping$$ of $$i+1$$ $$th$$ index and vice versa.\n\n### Case2 : \n**Valide case**\nCurrent element of nums1 and nums2 is smaller then to next element.\nHere there are two senarios :\n\n - If after swapping $$i$$ $$th$$ elements **Invalid case** is attained then we will have to swap $$i+1$$ $$th$$ elements too, else we can go with or without swapping $$i+1$$ $$th$$ elements it doesn\'t matter. \n\n\n# Complexity\n- Time complexity: **O(n)**\n\n- Space complexity: **O(n)**\n\n# Code\n```\nclass Solution {\npublic:\n\n    int solveTab(vector<int> &nums1, vector<int> &nums2){\n        int n = nums1.size();\n        vector<int>swap(n, 0); //considering swapping senario\n        vector<int>noSwap(n, 0); //considering not swapping \n        int ans=0;\n        if(nums1.back()<=nums1[n-2] || nums2.back()<=nums2[n-2] ) ans=1; \n        swap[n-1]=1; // base case\n\n        for(int i=n-2; i>=0; i--){\n            if(nums1[i]>=nums1[i+1] || nums2[i]>=nums2[i+1]){\n                //invalid case either swap i or swap i+1\n                swap[i]=1+noSwap[i+1];\n                noSwap[i]=swap[i+1];\n                ans=max(ans, min(swap[i], noSwap[i]));\n            }else{\n                //No swap needed\n                if(nums2[i]>=nums1[i+1] || nums1[i]>=nums2[i+1]){\n                    //If swapped it will become Invalid so you would have to swap again\n                    swap[i]=1+swap[i+1];\n                    noSwap[i]=noSwap[i+1];\n                }else{\n                    swap[i]=1+min(swap[i+1], noSwap[i+1]);\n                    noSwap[i]=min(swap[i+1], noSwap[i+1]);\n                }\n                ans=max(ans, min(swap[i], noSwap[i]));\n            }\n        }\n        return ans;\n    }\n\n    int minSwap(vector<int>& nums1, vector<int>& nums2) {\n        return solveTab(nums1, nums2);\n    }\n};\n```
['Dynamic Programming', 'C++']