Fair Distribution of Cookies

You are given an integer array cookies, where cookies[i] denotes the number of cookies in the i^th 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
Inputcookies = [8,15,10,20,8], k = 2
Output31
One optimal distribution is [8,15,8] and [10,20], whose child totals are 31 and 30, so the minimum possible unfairness is 31.
Example 2
Inputcookies = [6,1,3,2,2,4,1,2], k = 3
Output7
One optimal distribution is [6,1], [3,2,2], and [4,1,2], giving each child 7 cookies total, so the minimum possible unfairness is 7.

Constraints

  • 2 <= cookies.length <= 8
  • 1 <= cookies[i] <= 10^5
  • 2 <= k <= cookies.length

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate