EASY UNDERSTANDING DP (FOUR APPROACHES)
RECURSION \n\n \nint solve(vector<int>& nums1, vector<int>& nums2, int index, bool swapped)\n    {\n        if(index >= nums1.size())\n        {\n            re
**RECURSION **\n\n ```\nint solve(vector<int>& nums1, vector<int>& nums2, int index, bool swapped)\n    {\n        if(index >= nums1.size())\n        {\n            return 0;\n        }\n        \n        int ans = INT_MAX;\n        \n        int prev1 = nums1[index-1];\n        int prev2 = nums2[index-1];\n        \n        \n        //if only one element to swap or swap count ==1 \n        if(swapped)\n        {\n            swap(prev1, prev2);\n        }\n        \n        //Non-Swappped \n        if(nums1[index] > prev1 && nums2[index] > prev2)\n        {\n            ans = solve(nums1, nums2, index+1, 0);\n        }\n        \n        //swapped\n        if(nums1[index] > prev2 && nums2[index] > prev1)\n        {\n            ans = min(ans, 1 + solve(nums1, nums2, index+1, 1)); \n        }\n        \n        return ans;\n    }\n    \n```\n\n**MEMOIZATION **\n\n```\nint solveMem(vector<int>& nums1, vector<int>& nums2, int index, bool swapped, vector<vector<int>>& dp)\n    {\n        if(index >= nums1.size())\n        {\n            return 0;\n        }\n        \n         if(dp[index][swapped] != -1)\n         {\n             return dp[index][swapped];\n         }\n        \n        int ans = INT_MAX;\n        \n        int prev1 = nums1[index-1];\n        int prev2 = nums2[index-1];\n        \n        \n        //if only one element to swap or swap count ==1 \n        if(swapped)\n        {\n            swap(prev1, prev2);\n        }\n        \n        //Non-Swappped \n        if(nums1[index] > prev1 && nums2[index] > prev2)\n        {\n            ans = solveMem(nums1, nums2, index+1, 0, dp);\n        }\n        \n        //swapped\n        if(nums1[index] > prev2 && nums2[index] > prev1)\n        {\n            ans = min(ans, 1 + solveMem(nums1, nums2, index+1, 1,dp)); \n        }\n        \n        \n         return dp[index][swapped] = ans;\n    }\n    \n```\n\n**Tabulation**\n\n```\nint solveTab(vector<int>& nums1, vector<int>& nums2)\n    {\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        {\n            for(int swapped = 1; swapped >=0; swapped--)\n            {\n                int ans = INT_MAX;\n        \n        int prev1 = nums1[index-1];\n        int prev2 = nums2[index-1];\n        \n        \n        //if only one element to swap or swap count ==1 \n        if(swapped)\n        {\n            swap(prev1, prev2);\n        }\n        \n        //Non-Swappped \n        if(nums1[index] > prev1 && nums2[index] > prev2)\n        {\n            ans = dp[index+1][0];\n        }\n        \n        //swapped\n        if(nums1[index] > prev2 && nums2[index] > prev1)\n        {\n            ans = min(ans, 1 + dp[index+1][1]); \n        }\n    \n          dp[index][swapped] = ans;\n    \n            }\n        }\n    return dp[1][0];\n    }\n```\n\n**SPACE OPTIMIZATION**\n\n```\n int spaceOpti(vector<int>& nums1, vector<int>& nums2)\n    {\n        int swap = 0;\n        int noswap = 0;\n        int currSwap = 0;\n        int currNoSwap =0;\n         int n = nums1.size();\n      \n        for(int index = n-1; index >=1; index --)\n        {\n            for(int swapped = 1; swapped >=0; swapped--)\n            {\n                int ans = INT_MAX;\n        \n        int prev1 = nums1[index-1];\n        int prev2 = nums2[index-1];\n        \n        \n        //if only one element to swap or swap count ==1 \n        if(swapped)\n        {\n            int temp = prev2;\n            prev2 = prev1;\n            prev1 = temp;\n        }\n        \n        //Non-Swappped \n        if(nums1[index] > prev1 && nums2[index] > prev2)\n        {\n            ans = noswap;\n        }\n        \n        //swapped\n        if(nums1[index] > prev2 && nums2[index] > prev1)\n        {\n            ans = min(ans, 1 + swap); \n        }\n    \n          if(swapped)\n          {\n             currSwap = ans; \n          }\n                else{\n                    currNoSwap = ans;\n                }\n    \n            }\n            swap = currSwap;\n            noswap = currNoSwap;\n        }\n    return min(swap,noswap);\n    }``\n```\n\n# UPVOTE IF YOU FIND IT UNDERSTANDABLE******
['Recursion', 'Memoization', 'C']