> ## 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.

# Cutting Ribbons Python Solution with Tests

> Tested Python solution for LeetCode 1891 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 1891, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search). [View on LeetCode](https://leetcode.com/problems/cutting-ribbons/description/).

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

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

## Problem

You are given an integer array `ribbons`, where `ribbons[i]` represents the length of the i\<sup>th\</sup> ribbon, and an integer `k`. You may cut any of the ribbons into any number of segments of **positive integer** lengths, or perform no cuts at all.

* For example, if you have a ribbon of length `4`, you can:
  * Keep the ribbon of length `4`,
  * Cut it into one ribbon of length `3` and one ribbon of length `1`,
  * Cut it into two ribbons of length `2`,
  * Cut it into one ribbon of length `2` and two ribbons of length `1`, or
  * Cut it into four ribbons of length `1`.

Your task is to determine the **maximum** length of ribbon, `x`, that allows you to cut *at least* `k` ribbons, each of length `x`. You can discard any leftover ribbon from the cuts. If it is **impossible** to cut `k` ribbons of the same length, return 0.

### Examples

```
Input: ribbons = [9,7,5], k = 3
Output: 5
Explanation:
- Cut the first ribbon to two ribbons, one of length 5 and one of length 4.
- Cut the second ribbon to two ribbons, one of length 5 and one of length 2.
- Keep the third ribbon as it is.
Now you have 3 ribbons of length 5.
```

```
Input: ribbons = [7,5,9], k = 4
Output: 4
Explanation:
- Cut the first ribbon to two ribbons, one of length 4 and one of length 3.
- Cut the second ribbon to two ribbons, one of length 4 and one of length 1.
- Cut the third ribbon to three ribbons, two of length 4 and one of length 1.
Now you have 4 ribbons of length 4.
```

```
Input: ribbons = [5,7,9], k = 22
Output: 0
Explanation: You cannot obtain k ribbons of the same positive integer length.
```

### Constraints

* 1 \<= ribbons.length \<= 10\<sup>5\</sup>
* 1 \<= ribbons\[i] \<= 10\<sup>5\</sup>
* 1 \<= k \<= 10\<sup>9\</sup>

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log M) where n = len(ribbons), M = max(ribbons)
    # Space: O(1)
    def max_length(self, ribbons: list[int], k: int) -> int:
        left, right = 1, max(ribbons)
        while left <= right:
            mid = (left + right) // 2
            if sum(r // mid for r in ribbons) >= k:
                left = mid + 1
            else:
                right = mid - 1
        return right
```

## Complexity

| Time | Space |
| - | - |
| O(n log M) where n = len(ribbons), M = max(ribbons) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


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