Skip to content

Commit d229762

Browse files
authored
Create 1770-maximum-score-from-performing-multiplication-operations.js
1 parent c823a0f commit d229762

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number[]} multipliers
4+
* @return {number}
5+
*/
6+
const maximumScore = function(nums, multipliers) {
7+
const n = nums.length, m = multipliers.length
8+
const { max } = Math
9+
const dp = Array.from({ length: m + 1 }, () => Array(m + 1).fill(-Infinity))
10+
return helper(0, 0)
11+
function helper(l, i) {
12+
if(i === m) return 0
13+
if(dp[l][i] !== -Infinity) return dp[l][i]
14+
const pickLeft = helper(l + 1, i + 1) + nums[l] * multipliers[i]
15+
const pickRight = helper(l, i + 1) + nums[n - (i - l) - 1] * multipliers[i]
16+
return dp[l][i] = max(pickLeft, pickRight)
17+
}
18+
19+
};

0 commit comments

Comments
 (0)