-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1388.3n 块披萨.js
39 lines (36 loc) · 1.05 KB
/
1388.3n 块披萨.js
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
30
31
32
33
34
35
36
37
38
39
/**
* @param {number[]} slices
* @return {number}
*/
var maxSizeSlices = function(slices) {
const count = slices.length / 3;
let result = 0;
const dp = [];
for (let i = 0; i < count; i++) {
dp[i] = [];
let frontMax = 0;
for (let j = 0; j < slices.length - 1; j++) {
if (i === 0) {
dp[i][j] = slices[j];
} else {
frontMax = Math.max(frontMax, (dp[i - 1][j - 2] || 0));
dp[i][j] = Math.max(frontMax + slices[j]);
}
result = Math.max(result, dp[i][j]);
}
}
for (let i = 0; i < count; i++) {
dp[i] = [];
let frontMax = 0;
for (let j = 1; j < slices.length; j++) {
if (i === 0) {
dp[i][j - 1] = slices[j];
} else {
frontMax = Math.max(frontMax, (dp[i - 1][j - 3] || 0));
dp[i][j - 1] = Math.max(frontMax + slices[j]);
}
result = Math.max(result, dp[i][j - 1]);
}
}
return result;
};