Watch 10 video solutions for Fair Distribution of Cookies, a medium level problem involving Array, Dynamic Programming, Backtracking. This walkthrough by codestorywithMIK has 16,942 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an integer array cookies, where cookies[i] denotes the number of cookies in the ith bag. You are also given an integer k that denotes the number of children to distribute all the bags of cookies to. All the cookies in the same bag must go to the same child and cannot be split up.
The unfairness of a distribution is defined as the maximum total cookies obtained by a single child in the distribution.
Return the minimum unfairness of all distributions.
Example 1:
Input: cookies = [8,15,10,20,8], k = 2 Output: 31 Explanation: One optimal distribution is [8,15,8] and [10,20] - The 1st child receives [8,15,8] which has a total of 8 + 15 + 8 = 31 cookies. - The 2nd child receives [10,20] which has a total of 10 + 20 = 30 cookies. The unfairness of the distribution is max(31,30) = 31. It can be shown that there is no distribution with an unfairness less than 31.
Example 2:
Input: cookies = [6,1,3,2,2,4,1,2], k = 3 Output: 7 Explanation: One optimal distribution is [6,1], [3,2,2], and [4,1,2] - The 1st child receives [6,1] which has a total of 6 + 1 = 7 cookies. - The 2nd child receives [3,2,2] which has a total of 3 + 2 + 2 = 7 cookies. - The 3rd child receives [4,1,2] which has a total of 4 + 1 + 2 = 7 cookies. The unfairness of the distribution is max(7,7,7) = 7. It can be shown that there is no distribution with an unfairness less than 7.
Constraints:
2 <= cookies.length <= 81 <= cookies[i] <= 1052 <= k <= cookies.lengthProblem Overview: You are given an array where each element represents a bag of cookies and an integer k representing children. Each bag must go to exactly one child. The goal is to minimize unfairness, defined as the maximum number of cookies received by any child.
Approach 1: Backtracking with Pruning (Time: O(k^n), Space: O(n))
This approach tries every possible way to distribute cookie bags among k children using backtracking. Maintain an array where dist[i] tracks the total cookies assigned to the i-th child. For each bag, iterate through children and assign the bag recursively. After assigning all bags, compute the maximum value in dist and track the minimum possible unfairness.
Pruning significantly reduces the search space. If the current distribution already exceeds the best unfairness found so far, stop exploring that branch. Another common optimization skips assigning a bag to multiple children with the same current cookie count to avoid duplicate states. This approach is practical because the constraint on the number of bags is small.
Approach 2: Dynamic Programming with Bitmasking (Time: O(k * 3^n), Space: O(k * 2^n))
This approach models the problem using dynamic programming combined with bitmasking. Each bitmask represents a subset of cookie bags. First precompute the total cookies for every subset mask using bit operations. Then define dp[i][mask] as the minimum unfairness when distributing the bags represented by mask among i children.
For each state, iterate through all submasks of mask. Treat the submask as the set of bags assigned to the current child and combine it with the optimal distribution of the remaining bags. The unfairness becomes max(dp[i-1][mask ^ submask], subsetSum[submask]). Minimizing this value across all submasks gives the optimal distribution.
This method systematically explores subsets rather than permutations, making it easier to reason about overlapping subproblems. It performs well when the number of bags is small (typically ≤ 8), which keeps the bitmask state space manageable.
Recommended for interviews: The backtracking solution is usually expected. It demonstrates recursion, pruning, and state management. Interviewers want to see how you reduce exponential search with pruning. The bitmask dynamic programming approach is more advanced and shows deeper understanding of subset DP, which can stand out in system-level or algorithm-heavy interviews.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Backtracking with Pruning | O(k^n) | O(n) | Best practical solution when number of cookie bags is small. Easy to implement and preferred in interviews. |
| Dynamic Programming with Bitmask | O(k * 3^n) | O(k * 2^n) | Useful when practicing subset DP or when you want a structured state-based solution. |