Maximum Number of Groups Getting Fresh Donuts Problem
Description
LeetCode Problem 1815.
There is a donuts shop that bakes donuts in batches of batchSize. They have a rule where they must serve all of the donuts of a batch before serving any donuts of the next batch. You are given an integer batchSize and an integer array groups, where groups[i] denotes that there is a group of groups[i] customers that will visit the shop. Each customer will get exactly one donut.
When a group visits the shop, all customers of the group must be served before serving any of the following groups. A group will be happy if they all get fresh donuts. That is, the first customer of the group does not receive a donut that was left over from the previous group.
You can freely rearrange the ordering of the groups. Return the maximum possible number of happy groups after rearranging the groups.
Example 1:
Input: batchSize = 3, groups = [1,2,3,4,5,6]
Output: 4
Explanation: You can arrange the groups as [6,2,4,5,1,3]. Then the 1^st, 2^nd, 4^th, and 6^th groups will be happy.
Example 2:
Input: batchSize = 4, groups = [1,3,2,5,2,2,1,6]
Output: 4
Constraints:
1 <= batchSize <= 91 <= groups.length <= 301 <= groups[i] <= 10^9
Solution
Approach: Greedy + Bitmask Memoized Search
A group is happy when the number of customers served before it is a multiple of batchSize. So we want an order that makes as many prefix sums as possible divisible by batchSize. Only each group's size modulo batchSize matters.
- Groups whose size is a multiple of
batchSizedo not change the remainder, so put them first. Each one is happy, and they give us a starting answer. - The other groups have a remainder in
1 .. batchSize - 1. There are at most 30 groups andbatchSize <= 9, so we can store how many groups have each remainder in 5 bits each, 40 bits in total, packed into one 64-bit integerstate.
Define dfs(state, mod) as the most happy groups obtainable from the groups in state when the current prefix remainder is mod. Try every remainder i that is still available: take one such group, go to dfs(state without it, (mod + i) % batchSize), and keep the best result. If mod == 0, the group we place next is happy, so add 1. Memoize on state (the remainder mod is determined by the groups already used, so state alone identifies the subproblem).
The answer is the number of multiples plus dfs(state, 0).
Complexity: the number of distinct states is at most the product of (count + 1) over the remainders, which is a few million at worst; each state does O(batchSize) work. Space is proportional to the number of states.
from functools import lru_cache
class Solution:
def maxHappyGroups(self, batchSize: int, groups: List[int]) -> int:
@lru_cache(None)
def dfs(state, mod):
res = 0
x = int(mod == 0)
for i in range(1, batchSize):
if state >> (i * 5) & 31:
t = dfs(state - (1 << (i * 5)), (mod + i) % batchSize)
res = max(res, t + x)
return res
state = ans = 0
for v in groups:
i = v % batchSize
ans += i == 0
if i:
state += 1 << (i * 5)
ans += dfs(state, 0)
return ans