LeetCode 1891, Medium. Topics: Array, Binary Search. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 22 parametrized pytest cases, and a playground notebook:
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
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, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026