Maximum Score from Performing Multiplication Operations LeetCode solution in Java

1
class Solution {
2
public int maximumScore(int[] nums, int[] multipliers) {
3
int n = nums.length, m = multipliers.length;
4
return helper(nums, multipliers, 0, 0, n - 1, new Integer[m][m]);
5
}
6

7
public int helper(int[] nums, int[] multipliers, int idx, int left, int right, Integer[][] memo) {
8
if (idx == multipliers.length) return 0;
9
if (memo[idx][left] != null) return memo[idx][left];
10

11
int takeLeft =
12
nums[left] * multipliers[idx] + helper(nums, multipliers, idx + 1, left + 1, right, memo);
13

14
int takeRight =
15
nums[right] * multipliers[idx] + helper(nums, multipliers, idx + 1, left, right - 1, memo);
16

17
return memo[idx][left] = Math.max(takeLeft, takeRight);
18
}
19
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0