Longest Equal Cable Segment From Spools
Heaps, Sorting & SearchAuto-graded · 22 test casesLast updated
Find the largest integer segment length such that cutting every spool into equal segments yields at least k segments, using binary search on a monotonic feasibility check.
A workshop has several spools of cable. The i-th spool has a length of spools[i] units, where every length is a positive integer. You need to cut segments that all have the same positive integer length L.
Rules for cutting:
- Each spool is cut independently. A spool of length
syieldss // Lsegments of lengthL(integer division). Any remainder is discarded. - Segments from different spools cannot be joined together.
- You do not have to use every spool completely.
Given the list spools and an integer need, return the largest integer L >= 1 for which the total number of segments of length L across all spools is at least need.
If no positive integer length gives at least need segments (this happens exactly when the total length of all spools is less than need), return 0.
Constraints
1 <= len(spools) <= 2000001 <= spools[i] <= 10^91 <= need <= 10^12- The function returns a single integer.
Examples
Example 1
Input: longest_segment_length([9, 7, 5], 5)
Output: 3
Example 2
Input: longest_segment_length([5, 5, 5], 4)
Output: 2
Example 3
Input: longest_segment_length([1, 2, 3], 10)
Output: 0
Hints
Hint 1
For a fixed segment length L, how many segments can you get in total? Write it as a sum over the spools.
Hint 2
As L grows, the total number of segments never increases. What does that tell you about the set of feasible values of L?
Hint 3
Binary search L between 1 and the longest spool. Record the answer whenever a candidate is feasible, and move right; otherwise move left. If nothing is feasible, the answer stays 0.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Building the best length directly is awkward, but checking a given length is simple. For a candidate length L, the number of segments is
count(L) = sum(s // L for s in spools)
and L is feasible when count(L) >= need.
Why binary search works
When L increases, each term s // L stays the same or decreases, so count(L) is non-increasing. Therefore feasibility looks like True, True, ..., True, False, ..., False over L = 1, 2, 3, .... We want the last True, which is the classic binary search on the answer.
Bounds:
lo = 1, the smallest allowed length.hi = max(spools). AnyLabove this gives zero segments, andneed >= 1, so it cannot be feasible.
We keep best = 0. Whenever mid is feasible we set best = mid and search higher (lo = mid + 1); otherwise we search lower (hi = mid - 1). If even L = 1 fails, no value is ever recorded, so the function returns 0, which is the required result for the impossible case.
The feasibility check stops early once the running total reaches need, which saves work and keeps the counter small (relevant in fixed-width languages; Python integers do not overflow).
Complexity
- Time:
O(n log M)whereM = max(spools), about 30 iterations of anO(n)check. - Space:
O(1)extra.
Reference solution
from typing import List
def longest_segment_length(spools: List[int], need: int) -> int:
if not spools or need <= 0:
return 0
def feasible(seg_len: int) -> bool:
total = 0
for s in spools:
total += s // seg_len
if total >= need:
return True
return False
lo, hi = 1, max(spools)
best = 0
while lo <= hi:
mid = lo + (hi - lo) // 2
if feasible(mid):
best = mid
lo = mid + 1
else:
hi = mid - 1
return best