> For the complete documentation index, see [llms.txt](https://snowan.gitbook.io/study-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://snowan.gitbook.io/study-notes/leetcode/python/002305-fair-distribution-of-cookies.md).

# 2305-Fair-Distribution-of-Cookies

### Problem

<https://leetcode.com/problems/fair-distribution-of-cookies/description/>

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 <= 8 1 <= cookies\[i] <= 105 2 <= k <= cookies.length

### Solution

**Backtracking**

try all possible distribute ways to k children, and find the min unfairness.

```python
class Solution:
    def distributeCookies(self, cookies: List[int], k: int) -> int:
        min_unfair = float('inf')
        dist = [0] * k # ith child have number of cookies
        
        def backtrack(idx):
            nonlocal min_unfair, dist

            if idx == len(cookies):
                min_unfair = min(min_unfair, max(dist))
                return
            
            # exit early if current min_unfair already <= max(dist)
            if min_unfair <= max(dist):
                return
            
            # possible ways to distribute cookies 
            for i in range(k):
                dist[i] += cookies[idx]
                backtrack(idx+1)
                # backtracking
                dist[i] -= cookies[idx]

        backtrack(0)
        return min_unfair

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://snowan.gitbook.io/study-notes/leetcode/python/002305-fair-distribution-of-cookies.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
