-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.ts
29 lines (29 loc) · 883 Bytes
/
Solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function maximumScore(nums: number[], multipliers: number[]): number {
const inf = 1 << 30;
const n = nums.length;
const m = multipliers.length;
const f = new Array(m + 1).fill(0).map(() => new Array(m + 1).fill(-inf));
f[0][0] = 0;
let ans = -inf;
for (let i = 0; i <= m; ++i) {
for (let j = 0; j <= m - i; ++j) {
const k = i + j - 1;
if (i > 0) {
f[i][j] = Math.max(
f[i][j],
f[i - 1][j] + nums[i - 1] * multipliers[k],
);
}
if (j > 0) {
f[i][j] = Math.max(
f[i][j],
f[i][j - 1] + nums[n - j] * multipliers[k],
);
}
if (i + j === m) {
ans = Math.max(ans, f[i][j]);
}
}
}
return ans;
}