> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Divide Chocolate Python Solution with Tests

> Tested Python solution for LeetCode 1231 with 30 pytest cases. Generate a practice environment with lcpy.

LeetCode 1231, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search). [View on LeetCode](https://leetcode.com/problems/divide-chocolate/description/).

Generate this problem as a practice environment: tested reference solution, 30 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1231   # by problem number
lcpy gen -s divide_chocolate   # by problem name
```

## Problem

You have one chocolate bar that consists of some chunks. Each chunk has its own sweetness given by the array `sweetness`.

You want to share the chocolate with your `k` friends so you start cutting the chocolate bar into `k + 1` pieces using `k` cuts, each piece consists of some \<strong>consecutive\</strong> chunks.

Being generous, you will eat the piece with the \<strong>minimum total sweetness\</strong> and give the other pieces to your friends.

Find the \<strong>maximum total sweetness\</strong> of the piece you can get by cutting the chocolate bar optimally.

### Examples

```
Input: sweetness = [1,2,3,4,5,6,7,8,9], k = 5
Output: 6
Explanation: You can divide the chocolate to [1,2,3], [4,5], [6], [7], [8], [9]
```

```
Input: sweetness = [5,6,7,8,9,1,2,3,4], k = 8
Output: 1
Explanation: There is only one way to cut the bar into 9 pieces.
```

```
Input: sweetness = [1,2,2,1,2,2,1,2,2], k = 2
Output: 5
Explanation: You can divide the chocolate to [1,2,2], [1,2,2], [1,2,2]
```

### Constraints

* 0 \<= k \< sweetness.length \<= 10\<sup>4\</sup>
* 1 \<= sweetness\[i] \<= 10\<sup>5\</sup>

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/divide_chocolate/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/divide_chocolate/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * log(sum(sweetness)))
    # Space: O(1)
    def maximize_sweetness(self, sweetness: list[int], k: int) -> int:
        def can_eat(target: int) -> bool:
            current = pieces = 0
            for sweet in sweetness:
                current += sweet
                if current >= target:
                    current = 0
                    pieces += 1
            return pieces > k

        low, high = 0, sum(sweetness)
        while low < high:
            mid = (low + high + 1) // 2
            if can_eat(mid):
                low = mid
            else:
                high = mid - 1
        return low
```

## Complexity

| Time | Space |
| - | - |
| O(n \* log(sum(sweetness))) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.