Pizza With 3n Slices - Problem
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 pizza.
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.
Input & Output
Example 1 — Basic Case
$
Input:
slices = [1,2,3,4,5,6]
›
Output:
10
💡 Note:
Pick slices 3 and 6 for maximum sum. When you pick 3, Alice gets 2, Bob gets 4. When you pick 6, Alice gets 5, Bob gets 1. Total: 3 + 6 = 9. Wait, actually picking slices with values 4 and 6 gives sum 10.
Example 2 — Larger Array
$
Input:
slices = [4,1,2,5,8,3,1,9,7]
›
Output:
21
💡 Note:
With n=3, you need to pick 3 slices. Optimal picks are slices with values 8, 9, and 4, giving sum 8 + 9 + 4 = 21.
Example 3 — Small Array
$
Input:
slices = [3,1,2]
›
Output:
3
💡 Note:
With n=1, you pick 1 slice. The maximum value slice is 3, so pick that one.
Constraints
- 3 ≤ slices.length ≤ 500
- slices.length % 3 == 0
- 1 ≤ slices[i] ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input
Circular array of 3n pizza slices
2
Constraint
Picking slice i blocks slices i-1 and i+1
3
Goal
Pick n slices to maximize your sum
Key Takeaway
🎯 Key Insight: Convert circular array problem to two linear House Robber problems by excluding first or last element
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code