Longest Uniform Cut Length from Rope Pieces
Heaps, Sorting & SearchAuto-graded · 23 test casesLast updated
Find the largest integer length such that cutting every rope into pieces of that length yields at least k pieces; tests binary search over a monotonic answer space.
Longest Uniform Cut Length
A workshop has a list of ropes, where ropes[i] is the length of the i-th rope (a positive integer). You are also given a target count k.
You want to produce at least k pieces that all have exactly the same integer length L. Each rope can be cut into as many pieces of length L as fit, and whatever remains of that rope (shorter than L) is thrown away. Ropes cannot be tied together, so a piece always comes from a single rope.
Return the largest integer L >= 1 for which the ropes together provide at least k pieces of length L. If no such positive length exists (even L = 1 gives fewer than k pieces), return 0.
Function
Implement longest_uniform_cut(ropes, k), which takes the list of rope lengths and the integer k, and returns a single integer.
Constraints
1 <= len(ropes) <= 10^51 <= ropes[i] <= 10^91 <= k <= 10^9- The result is an integer; return
0when it is impossible to getkpieces of length at least 1.
Examples
Example 1
Input: longest_uniform_cut([5, 9, 7], 4)
Output: 4
Example 2
Input: longest_uniform_cut([5, 9, 7], 3)
Output: 5
Example 3
Input: longest_uniform_cut([2, 3], 10)
Output: 0
Hints
Hint 1
For a fixed length L, a rope of length w gives exactly w // L pieces. Summing this over all ropes tells you whether L works.
Hint 2
As L grows, the total number of pieces never increases. So the lengths that work form a prefix of 1, 2, 3, ... and you want the end of that prefix.
Hint 3
Binary search on L between 1 and max(ropes). Record L whenever it is feasible and then try larger; otherwise try smaller. If nothing is feasible, the answer stays 0.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Instead of searching through the input, search through the possible answers. For a candidate length L, a rope of length w produces w // L pieces (the remainder is shorter than L and discarded). So L is feasible when sum(r // L for r in ropes) >= k.
This count never increases as L increases, so feasibility is monotonic: all small lengths up to some threshold are feasible, and all larger ones are not. We want the last feasible length, which binary search finds.
- Search range:
lo = 1(pieces must have positive length) andhi = max(ropes)(any longer length yields zero pieces, andk >= 1). - At each step, test
mid. If feasible, record it as the best answer and search higher (lo = mid + 1). Otherwise search lower (hi = mid - 1). - If no length is feasible,
bestkeeps its initial value0, which is exactly the required output.
Why it is correct
Every length below lo that has been examined is feasible or has a feasible length at or above it recorded, and every length above hi is known to be infeasible by monotonicity. When lo > hi all lengths have been classified, so best is the largest feasible length, or 0 if none exists.
The counting loop stops early once the total reaches k, since only the comparison with k matters. This does not change the result.
Complexity
- Time:
O(n log M)wheren = len(ropes)andM = max(ropes), about 30 passes over at most10^5elements. - Space:
O(1)extra.
Reference solution
from typing import List
def longest_uniform_cut(ropes: List[int], k: int) -> int:
lo, hi = 1, max(ropes)
best = 0
while lo <= hi:
mid = (lo + hi) // 2
pieces = 0
for r in ropes:
pieces += r // mid
if pieces >= k:
break
if pieces >= k:
best = mid
lo = mid + 1
else:
hi = mid - 1
return best