Pizza With 3n Slices Problem
Description
LeetCode Problem 1388.
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:

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] <= 1000
Solution
Approach: Non-Adjacent Picks on a Circle (DP)
Each time you pick a slice, its two neighbours go to Alice and Bob. So the slices you end up with are never next to each other. The key fact is the converse: any choice of n pairwise non-adjacent slices out of the 3n can actually be taken.
Why: the 2n unchosen slices fall into n gaps between consecutive chosen slices, each gap of size at least 1. They cannot all be of size 1 (that would be only n slices), so some chosen slice has a gap of at least 2 on one side. Pick that slice first. Alice and Bob take one slice from each neighbouring gap, those two gaps merge into one of size at least 1, and the remaining 3(n - 1) slices form a smaller circle in which the other chosen slices are still non-adjacent. Repeat until all are taken.
So the problem becomes: choose n values from a circular array of length 3n, no two adjacent, with the largest sum.
The first and last slices are neighbours on the circle, so they cannot both be chosen. Solve the problem on a line twice, once without the first slice and once without the last, and take the better result.
For a line nums of length m, let f[i][j] be the best sum of at most j non-adjacent values among the first i:
- skip value
i:f[i - 1][j], - take value
i:f[i - 2][j - 1] + nums[i - 1](the value right before it must be skipped).
f[i][j] is the larger of the two, and the answer for the line is f[m][n]. "At most" is the same as "exactly" here, because all sizes are positive and a line of 3n - 1 values always has room for one more non-adjacent pick.
Complexity: O(n2) time and O(n2) space, where n is the length of slices.
class Solution:
def maxSizeSlices(self, slices: List[int]) -> int:
n = len(slices) // 3
# The best sum of n pairwise non-adjacent values of a line (not a circle).
def best_line(nums: List[int]) -> int:
m = len(nums)
# f[i][j]: the best sum of at most j non-adjacent values among the first i
f = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
take = (f[i - 2][j - 1] if i >= 2 else 0) + nums[i - 1]
f[i][j] = max(f[i - 1][j], take)
return f[m][n]
# On the circle the first and last slices are neighbours: drop one of them.
return max(best_line(slices[1:]), best_line(slices[:-1]))