Maximum Score from Performing Multiplication Operations - LeetCode
You are given two integer arrays nums and multipliers of size n and m respectively, where n >= m. The arrays are 1-indexed. You begin with a score of 0. You want to perform exactly m operations. On the ith operation (1-indexed), you will:
Choose one integer x from either the start or the end of the array nums. Add multipliers[i] * x to your score. Remove x from the array nums.Return __the maximum score after performing __m operations.
Example 1: Input: nums = [1,2,3], multipliers = [3,2,1] Output: 14 Explanation: An optimal solution is as follows: - Choose from the end, [1,2,3], adding 3 * 3 = 9 to the score. - Choose from the end, [1,2], adding 2 * 2 = 4 to the score. - Choose from the end, [1], adding 1 * 1 = 1 to the score. The total score is 9 + 4 + 1 = 14.
Example 2: Input: nums = [-5,-3,-3,-2,7,1], multipliers = [-10,-5,3,4,6] Output: 102 Explanation: An optimal solution is as follows: - Choose from the start, [-5,-3,-3,-2,7,1], adding -5 * -10 = 50 to the score. - Choose from the start, [-3,-3,-2,7,1], adding -3 * -5 = 15 to the score. - Choose from the start, [-3,-2,7,1], adding -3 * 3 = -9 to the score. - Choose from the end, [-2,7,1], adding 1 * 4 = 4 to the score. - Choose from the end, [-2,7], adding 7 * 6 = 42 to the score. The total score is 50 + 15 - 9 + 4 + 42 = 102.
Constraints:
n == nums.length
m == multipliers.length
1 <= m <= 103
m <= n <= 105
-1000 <= nums[i], multipliers[i] <= 1000
- code
class Solution {
private int[][] memo;
private int[] nums, multipliers;
private int n, m;
private int dp(int i, int left) {
if (i == m) {
return 0; // Base case
}
int mult = multipliers[i];
int right = n - 1 - (i - left);
if (memo[i][left] == 0) {
// Recurrence relation
memo[i][left] = Math.max(mult * nums[left] + dp(i + 1, left + 1),
mult * nums[right] + dp(i + 1, left));
}
return memo[i][left];
}
public int maximumScore(int[] nums, int[] multipliers) {
n = nums.length;
m = multipliers.length;
this.nums = nums;
this.multipliers = multipliers;
this.memo = new int[m][m];
return dp(0, 0);
}
}
- code why backwards? from top-down we observer the base case is i ==m, so we need to calculate i == m at first. We always need to touch base case at first in bottom up to calculate further.
class Solution:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
n, m = len(nums), len(multipliers)
dp = [[0] * (m + 1) for _ in range(m + 1)]
for i in range(m - 1, -1, -1):
for left in range(i, -1, -1):
mult = multipliers[i]
right = n - 1 - (i - left)
dp[i][left] = max(mult * nums[left] + dp[i + 1][left + 1],
mult * nums[right] + dp[i + 1][left])
return dp[0][0]
- code TLE
class Solution:
def maximumScore(self, nums: List[int], multipliers: List[int]) -> int:
# # @lru_cache(maxsize=None)
def rec(start_i, m_index, memo):
if (start_i, m_index) in memo: return memo[(start_i, m_index)]
if m_index == len(multipliers): return 0
# end_i - start_i + m = len(nums) - 1
end_i = len(nums) - 1 - m_index + start_i
# pickstart = multipliers[m_index] * nums[start_i] + rec(start_i + 1, m_index + 1, memo)
# pickend = multipliers[m_index] * nums[end_i] + rec(start_i, m_index + 1, memo)
memo[(start_i, m_index)] = max(multipliers[m_index] * nums[start_i] + rec(start_i + 1, m_index + 1, memo), multipliers[m_index] * nums[end_i] + rec(start_i, m_index + 1, memo))
return memo[(start_i, m_index)]
memo = {}
return rec(0, 0, memo)