630. Fair Cookie Split
A baker has packed her cookies into bags; bags[i] is the number of cookies in bag i. Bags are sealed, so each bag must be given whole to exactly one of k children. A child may receive no bag at all. The unfairness of a hand-out is the largest number of cookies received by any single child.
Return the smallest unfairness over all possible hand-outs. Trying every assignment of bags to children costs k^n steps; use a bit mask of the bags already given away to do much better.
Example 1
- Input:
- bags = [8,15,10,20,8], k = 2
- Output:
- 31
- Explanation:
Give one child the bags with 8, 8 and 15 cookies (31 in total) and the other child the bags with 10 and 20 (30). The larger share is 31, and no split does better.
Example 2
- Input:
- bags = [5,5,5,5], k = 4
- Output:
- 5
- Explanation:
Each child gets one bag of 5 cookies.
Example 3
- Input:
- bags = [6,3,3], k = 1
- Output:
- 12
- Explanation:
With one child, that child gets everything: 12 cookies.
Constraints
- 1 ≤
bags.length≤ 10 - 1 ≤
k≤bags.length - 1 ≤
bags[i]≤ 105
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms
Expected complexity
- Time
- O(k * 3^n)
- Space
- O(2^n)