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.lengthThis approach leverages backtracking to explore all possible ways of distributing the bags of cookies among the children. We utilize a recursive function to attempt to give each bag of cookies to any of the available children. At each step, we update the total cookies received by the child to whom the current bag is assigned. The goal is to minimize the maximum number of cookies any child receives, known as unfairness.
This solution uses recursion to assign each cookie bag to one of the k children. Each recursive call explores adding the current cookie bag to any child's current collection. The recursion depth corresponds to processing each cookie bag. Once all bags are distributed, it computes the maximum cookies received by any child in that distribution (unfairness). The recursive solution uses backtracking to explore different paths and aims to minimize the maximum unfairness across all potential distributions.
C++
Java
Python
C#
JavaScript
Time Complexity: O(k^n), where n is the number of cookie bags and k is the number of children. The solution explores every distribution possibility.
Space Complexity: O(k + n), due to the recursion stack and the distribution array.
This approach uses bitmasking in conjunction with dynamic programming to keep track of all combinations of cookie distributions among k children. We aim to optimize processing by encoding choices using bitmasks to represent different subsets of cookies. This method alleviates computational overhead by avoiding permutation/combination evaluation via iterative states and sub-state record tracking. We leverage a DP technique where dp[mask][children] represents the minimum unfairness achievable assigning the selected cookie combination (mask) across given children.
The C solution here employs a 2D array to track the minimal unfairness through a combination of subsets and children allocation states. Utilizes numerous possible sub-states via masks representing cookie allocation realization across valid compartments. Varied subsets are computed iteratively ensuring that the unnecessary re-evaluation of states is avoided through DP caching.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n * 2^n * k), dealing with both subset and child assignment constraints.
Space Complexity: O(2^n * k), necessary for complete subset and branch caching.
| Approach | Complexity |
|---|---|
| Backtracking Approach | Time Complexity: O(k^n), where n is the number of cookie bags and k is the number of children. The solution explores every distribution possibility. |
| Dynamic Programming via Bitmasking | Time Complexity: O(n * 2^n * k), dealing with both subset and child assignment constraints. |
Fair Distribution of Cookies | Khandani Backtracking Template | Leetcode-2305 | Explanation | Coding • codestorywithMIK • 11,315 views views
Watch 9 more video solutions →Practice Fair Distribution of Cookies with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor