1
var maxSizeSlices = function (slices) {2
const numSlices = slices.length / 3;3
const len = slices.length - 1;5
const dp = new Array(len)7
.map(() => new Array(numSlices + 1).fill(0));8
const getMaxTotalSlices = (pieces) => {9
// the max for 1 piece using only the first slice is itself11
// the max for 1 piece using the first 2 slices is the max of the first and second slice12
dp[1][1] = Math.max(pieces[0], pieces[1]);13
// start the max as the max of taking 1 slice from the first 2 slices16
// calculate the max value for taking x number of pieces using up to that piece17
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 piece21
dp[i - 2][numPieces - 1] + pieces[i]22
); // the max for taking this piece23
if (max < dp[i][numPieces]) max = dp[i][numPieces]; // update the max if it is greater30
getMaxTotalSlices(slices.slice(0, slices.length - 1)), // get max without the last slice31
getMaxTotalSlices(slices.slice(1))32
); // get max without the first slice