Imagine you're managing a donut shop that operates with a unique serving system! The shop bakes donuts in fixed batches of size batchSize, and must serve all donuts from one batch before starting the next.
You have multiple customer groups coming to your shop, where groups[i] represents the number of customers in the i-th group. Each customer gets exactly one donut, and all customers in a group must be served together before serving the next group.
Here's the key: A group is "happy" if they get fresh donuts - meaning the first customer in their group doesn't receive a leftover donut from the previous group's batch.
Your goal is to rearrange the order of groups to maximize the number of happy groups. Can you figure out the optimal arrangement?
Example: If batchSize = 3 and groups = [1, 2, 3, 4, 5, 6], you need to strategically order these groups so that as many as possible start with a fresh batch!
Input & Output
Visualization
Time & Space Complexity
In practice much better due to memoization and state pruning
Space for memoization table storing unique states
Constraints
- 1 โค batchSize โค 9
- 1 โค groups.length โค 30
- 1 โค groups[i] โค 109
- Key insight: Only remainders modulo batchSize matter, not actual group sizes