Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows:
Given an integer array slices that represent the sizes of the pizza slices in a clockwise direction, return the maximum possible sum of slice sizes that you can pick.
Example 1:
Input: slices = [1,2,3,4,5,6] Output: 10 Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6.
Example 2:
Input: slices = [8,9,8,6,1,1] Output: 16 Explanation: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8.
Constraints:
3 * n == slices.length1 <= slices.length <= 5001 <= slices[i] <= 1000Problem summary: There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows: You will pick any pizza slice. Your friend Alice will pick the next slice in the anti-clockwise direction of your pick. Your friend Bob will pick the next slice in the clockwise direction of your pick. Repeat until there are no more slices of pizzas. Given an integer array slices that represent the sizes of the pizza slices in a clockwise direction, return the maximum possible sum of slice sizes that you can pick.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Greedy
[1,2,3,4,5,6]
[8,9,8,6,1,1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1388: Pizza With 3n Slices
class Solution {
private int n;
public int maxSizeSlices(int[] slices) {
n = slices.length / 3;
int[] nums = new int[slices.length - 1];
System.arraycopy(slices, 1, nums, 0, nums.length);
int a = g(nums);
System.arraycopy(slices, 0, nums, 0, nums.length);
int b = g(nums);
return Math.max(a, b);
}
private int g(int[] nums) {
int m = nums.length;
int[][] f = new int[m + 1][n + 1];
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
f[i][j] = Math.max(f[i - 1][j], (i >= 2 ? f[i - 2][j - 1] : 0) + nums[i - 1]);
}
}
return f[m][n];
}
}
// Accepted solution for LeetCode #1388: Pizza With 3n Slices
func maxSizeSlices(slices []int) int {
n := len(slices) / 3
g := func(nums []int) int {
m := len(nums)
f := make([][]int, m+1)
for i := range f {
f[i] = make([]int, n+1)
}
for i := 1; i <= m; i++ {
for j := 1; j <= n; j++ {
f[i][j] = max(f[i-1][j], nums[i-1])
if i >= 2 {
f[i][j] = max(f[i][j], f[i-2][j-1]+nums[i-1])
}
}
}
return f[m][n]
}
a, b := g(slices[:len(slices)-1]), g(slices[1:])
return max(a, b)
}
# Accepted solution for LeetCode #1388: Pizza With 3n Slices
class Solution:
def maxSizeSlices(self, slices: List[int]) -> int:
def g(nums: List[int]) -> int:
m = len(nums)
f = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
f[i][j] = max(
f[i - 1][j], (f[i - 2][j - 1] if i >= 2 else 0) + nums[i - 1]
)
return f[m][n]
n = len(slices) // 3
a, b = g(slices[:-1]), g(slices[1:])
return max(a, b)
// Accepted solution for LeetCode #1388: Pizza With 3n Slices
/**
* [1388] Pizza With 3n Slices
*
* There is a pizza with 3n slices of varying size, you and your friends will take slices of pizza as follows:
*
* You will pick any pizza slice.
* Your friend Alice will pick the next slice in the anti-clockwise direction of your pick.
* Your friend Bob will pick the next slice in the clockwise direction of your pick.
* Repeat until there are no more slices of pizzas.
*
* Given an integer array slices that represent the sizes of the pizza slices in a clockwise direction, return the maximum possible sum of slice sizes that you can pick.
*
* Example 1:
* <img alt="" src="https://assets.leetcode.com/uploads/2020/02/18/sample_3_1723.png" style="width: 500px; height: 266px;" />
* Input: slices = [1,2,3,4,5,6]
* Output: 10
* Explanation: Pick pizza slice of size 4, Alice and Bob will pick slices with size 3 and 5 respectively. Then Pick slices with size 6, finally Alice and Bob will pick slice of size 2 and 1 respectively. Total = 4 + 6.
*
* Example 2:
* <img alt="" src="https://assets.leetcode.com/uploads/2020/02/18/sample_4_1723.png" style="width: 500px; height: 299px;" />
* Input: slices = [8,9,8,6,1,1]
* Output: 16
* Explanation: Pick pizza slice of size 8 in each turn. If you pick slice with size 9 your partners will pick slices of size 8.
*
*
* Constraints:
*
* 3 * n == slices.length
* 1 <= slices.length <= 500
* 1 <= slices[i] <= 1000
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/pizza-with-3n-slices/
// discuss: https://leetcode.com/problems/pizza-with-3n-slices/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
// Credit: https://leetcode.com/problems/pizza-with-3n-slices/solutions/3107696/just-a-runnable-solution/
pub fn max_size_slices(slices: Vec<i32>) -> i32 {
let m = slices.len();
let n = m / 3;
let slices1 = slices[0..m - 1].to_vec();
let slices2 = slices[1..m].to_vec();
let val1 = Self::max_sum(&slices1, n);
let val2 = Self::max_sum(&slices2, n);
std::cmp::max(val1, val2)
}
fn max_sum(slices: &Vec<i32>, n: usize) -> i32 {
let m = slices.len();
let mut dp = vec![vec![0; n + 1]; m + 1];
for i in 1..=m {
for j in 1..=n {
if i == 1 {
dp[i][j] = slices[0];
} else {
dp[i][j] = std::cmp::max(dp[i - 1][j], dp[i - 2][j - 1] + slices[i - 1]);
}
}
}
dp[m][n]
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1388_example_1() {
let slices = vec![1, 2, 3, 4, 5, 6];
let result = 10;
assert_eq!(Solution::max_size_slices(slices), result);
}
#[test]
fn test_1388_example_2() {
let slices = vec![8, 9, 8, 6, 1, 1];
let result = 16;
assert_eq!(Solution::max_size_slices(slices), result);
}
}
// Accepted solution for LeetCode #1388: Pizza With 3n Slices
function maxSizeSlices(slices: number[]): number {
const n = Math.floor(slices.length / 3);
const g = (nums: number[]): number => {
const m = nums.length;
const f: number[][] = Array(m + 1)
.fill(0)
.map(() => Array(n + 1).fill(0));
for (let i = 1; i <= m; ++i) {
for (let j = 1; j <= n; ++j) {
f[i][j] = Math.max(f[i - 1][j], (i > 1 ? f[i - 2][j - 1] : 0) + nums[i - 1]);
}
}
return f[m][n];
};
const a = g(slices.slice(0, -1));
const b = g(slices.slice(1));
return Math.max(a, b);
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.