c++ | RECURSION + MEMOIZATION | (DP Approach)
Brute Force Recursion Approach\n\nint bruteForce()(vector<int>& v1, vector<int>& v2, int i){\n\tint n = v1.size();\n\tif(i>1 && (v1[i-2]>= v1[i-1] || v2[i-2]>=v
**Brute Force Recursion Approach**\n```\nint bruteForce()(vector<int>& v1, vector<int>& v2, int i){\n\tint n = v1.size();\n\tif(i>1 && (v1[i-2]>= v1[i-1] || v2[i-2]>=v2[i-1])) return 1e6;\n\tif(i==n) return 0;\n\n\t// Swap for current index so 1 + getMinSwap(...);\n\tint min_swaps = INT_MAX;\n\t// swapping for current index \n\tswap(v1[i], v2[i]);\n\tmin_swaps = 1 + getMinSwaps(v1, v2, i+1, 1);\n\n\n\t// reverting the array to form it was in \n\t// before doing the above swapping call\n\tswap(v1[i], v2[i]);\n\n\t// dont swap current for current index\n\treturn min_swaps = min(min_swaps, getMinSwaps(v1, v2, i+1, 0));\n}\n```\n    \n    \n**   RECURSION + MEMOIZATION   **      \n    \n```\nint dp[100005][2];\n\nint getMinSwaps(vector<int>& v1, vector<int>& v2, int i, int p = 0){\n\tint n = v1.size();\n\tif(i>1 && (v1[i-2]>= v1[i-1] || v2[i-2]>=v2[i-1])) return 1e6;\n\tif(i==n) return 0;\n\tif(dp[i][p] != -1) return dp[i][p];\n\n\n\t// The two states that are changing are \n\t// 1) index (i.e. i)\n\t// 2) swapping for current index (i.e. p)\n\t// so if swapped than p = 1 \n\t// else p = 0\n\tswap(v1[i], v2[i]);\n\t// swapped so p = 1\n\tdp[i][p] = 1 + getMinSwaps(v1, v2, i+1, 1);\n\tswap(v1[i], v2[i]);\n\n\t// no swapped so p = 0\n\treturn dp[i][p] = min(dp[i][p], getMinSwaps(v1, v2, i+1, 0));\n}\n```\n\n
['Dynamic Programming', 'Recursion', 'Memoization', 'C']