Skip to content

Commit 1ba426b

Browse files
authored
Update 1388-pizza-with-3n-slices.js
1 parent 09a28de commit 1ba426b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

1388-pizza-with-3n-slices.js

+29
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,32 @@ function maxSum(arr, n) {
3636
}
3737
return dp[m][n]
3838
}
39+
40+
41+
// another
42+
43+
/**
44+
* @param {number[]} slices
45+
* @return {number}
46+
*/
47+
const maxSizeSlices = function (slices) {
48+
const n = slices.length, m = ~~(n / 3)
49+
const arr1 = slices.slice(1), arr2 = slices.slice(0, n - 1)
50+
return Math.max(helper(arr1, m), helper(arr2, m))
51+
function helper(arr, k) {
52+
const len = arr.length
53+
const dp = Array.from({ length: len + 1 }, () => Array(k + 1).fill(0))
54+
for(let i = 1; i <= len; i++) {
55+
for(let j = 1; j <= k; j++) {
56+
if(i === 1) dp[i][j] = arr[i - 1]
57+
else {
58+
dp[i][j] = Math.max(
59+
dp[i - 1][j],
60+
dp[i - 2][j - 1] + arr[i - 1]
61+
)
62+
}
63+
}
64+
}
65+
return dp[len][k]
66+
}
67+
}

0 commit comments

Comments
 (0)