Skip to content

Commit 528036a

Browse files
committed
Runtime: 93 ms (Top 88.52%) | Memory: 64.00 MB (Top 63.93%)
1 parent a954dd3 commit 528036a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Runtime: 93 ms (Top 88.52%) | Memory: 64.00 MB (Top 63.93%)
2+
3+
/**
4+
* @param {number[]} nums
5+
* @param {number[]} multipliers
6+
* @return {number}
7+
*/
8+
var maximumScore = function(nums, multipliers) {
9+
// using an array as memo will be faster
10+
// in processing than using an object
11+
let memo = new Array(multipliers.length+1).fill(0)
12+
.map(() => new Array(multipliers.length+1).fill(0));
13+
14+
// starting point will be from the end
15+
for (let i = multipliers.length - 1; i >= 0; i--) {
16+
// at state i, used i elements from nums
17+
// and left nums.length - i consecutive elements
18+
// those consecutive elements can be seen as the rest block
19+
// the left of the rest block can be any where from i to 0
20+
// whereas the left element that this state can use is from the rest block
21+
// so left = i to 0;
22+
for (let left = i; left >= 0; left--) {
23+
let right = (nums.length - 1) - (i - left);
24+
25+
// calculate all possible cases for state i
26+
// each case will combine with each case of state i - 1 and so on
27+
memo[i][left] = Math.max(
28+
multipliers[i] * nums[left] + memo[i+1][left+1],
29+
multipliers[i] * nums[right] + memo[i+1][left]
30+
);
31+
}
32+
}
33+
34+
// finish the calculation and
35+
// return the last case that we calculated
36+
return memo[0][0];
37+
}

0 commit comments

Comments
 (0)