1
var maxSizeSlices = function (slices) {
2
const numSlices = slices.length / 3;
3
const len = slices.length - 1;
4

5
const dp = new Array(len)
6
.fill(null)
7
.map(() => new Array(numSlices + 1).fill(0));
8
const getMaxTotalSlices = (pieces) => {
9
// the max for 1 piece using only the first slice is itself
10
dp[0][1] = pieces[0];
11
// the max for 1 piece using the first 2 slices is the max of the first and second slice
12
dp[1][1] = Math.max(pieces[0], pieces[1]);
13
// start the max as the max of taking 1 slice from the first 2 slices
14
let max = dp[1][1];
15

16
// calculate the max value for taking x number of pieces using up to that piece
17
for (let i = 2; i < pieces.length; i++) {
18
for (let numPieces = 1; numPieces <= numSlices; numPieces++) {
19
dp[i][numPieces] = Math.max(
20
dp[i - 1][numPieces], // the max for not taking this piece
21
dp[i - 2][numPieces - 1] + pieces[i]
22
); // the max for taking this piece
23
if (max < dp[i][numPieces]) max = dp[i][numPieces]; // update the max if it is greater
24
}
25
}
26
return max;
27
};
28

29
return Math.max(
30
getMaxTotalSlices(slices.slice(0, slices.length - 1)), // get max without the last slice
31
getMaxTotalSlices(slices.slice(1))
32
); // get max without the first slice
33
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0