category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

1770. Maximum Score from Performing Multiplication Operations

Top down implementation

some key points:
Approach
Wrong Implementation ```cpp #define vvi vector<vector> #define vi vector class Solution { public: vvi dp; int n, m; vector nums, multipliers; int fun(int i, int l, int r) { // printf("%d %d %d \n", i, l, r); if (i >= m) return 0; if (l > r) return 0; int &one = dp[i][l]; int &two = dp[i][r]; if (one != -1 and two != -1) return max(one, two); else if (one == -1 and two != -1) return two; else if (one != -1 and two == -1) return one; else { int one = fun(i + 1, l + 1, r) + nums[l] * multipliers[i]; int two = fun(i + 1, l, r - 1) + nums[r] * multipliers[i]; return max(one, two); } } int maximumScore(vector& nums, vector& multipliers) { n = nums.size(); m = multipliers.size(); this -> nums = nums; this -> multipliers = multipliers; dp = vvi(m + 1, vi(n + 1, -1)); return fun(0, 0, n - 1); } }; ``` </details>
Top-Down implementation ```cpp class Solution { public: vector<vector> dp; int n, m; vector nums, multipliers; int fun(int i, int l) { if (i >= m) return 0; int r = (n - 1) - (i - l); if (l > r) return 0; int &ans = dp[i][l]; if (ans != -1) return ans; ans = max(fun(i + 1, l + 1) + nums[l] * multipliers[i], fun(i + 1, l) + nums[r] * multipliers[i]); return ans; } int maximumScore(vector& nums, vector& multipliers) { n = nums.size(); m = multipliers.size(); this -> nums = nums; this -> multipliers = multipliers; dp = vector<vector> (m + 1, vi(m + 1, -1)); return fun(0, 0); } }; ``` </details> #### Bottom up implementation - it is often harder to implement, because the order in which we iterate needs to be precise. - we need to iterate backwards starting from m (because the base case happens when i equals m). - We also need to initialize DP with one extra row so that we don't go out of bounds in the first iteration of the outer loop. - Try to apply loops in such a way to keep minimise distance from the `BASE CASES`, we have to build the solution upon the base case.
code implementation ```cpp class Solution { public: int maximumScore(vector& nums, vector& multipliers) { int n = nums.size(); int m = multipliers.size(); vector<vector> dp(m + 1, vector(m + 1, 0)); for (int i = m - 1; i >= 0; i--) { for (int left = i; left >= 0; left --) { int right = (n - 1) - (i - left); dp[i][left] = max(dp[i + 1][left + 1] + nums[left] * multipliers[i], dp[i + 1][left] + nums[right] * multipliers[i]); } } return dp[0][0]; } }; ``` </details>