Maximize Uniform Cut Length for Target Segment Count
Heaps, Sorting & SearchAuto-graded · 25 test casesLast updated
Find the maximum integer segment length that can produce at least a specified count of equal-sized pieces using binary search on answer.
You are given a list of positive integers a representing the initial lengths of several materials, and a target integer k representing the minimum number of identical segments required.
Your task is to cut the materials into segments of a fixed integer length $L$. Each original material piece can be cut into zero or more segments of length $L$. Any remainder less than $L$ is discarded, and pieces cannot be joined together.
Determine the maximum possible integer length $L \ge 1$ such that the total count of segments of length $L$ produced across all materials is at least $k$.
If k <= 0, return the maximum value present in a (or 0 if a is empty).
If it is impossible to obtain at least k segments of length $L \ge 1$, return 0.
Constraints
0 <= len(a) <= 10^51 <= a[i] <= 10^9for each element ina-10^9 <= k <= 10^9
Examples
Example 1
Input: max_wood_length([5, 9, 7], 5)
Output: 3
Example 2
Input: max_wood_length([5, 9, 7], 20)
Output: 1
Example 3
Input: max_wood_length([4, 4, 4], 3)
Output: 4
Hints
Hint 1
Notice that if a segment length $L$ yields at least $k$ pieces, any smaller length $L' < L$ will yield at least as many pieces.
Hint 2
The maximum plausible length is `max(a)`. This allows binary searching for $L$ in the range `[1, max(a)]`.
Hint 3
For a candidate length `mid`, compute the total segments as `sum(x // mid for x in a)`. If this sum is at least $k$, try larger values of `mid`.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem can be solved using Binary Search on Answer.
Let $f(L)$ be the total number of segments of length $L$ we can obtain from the array a:
$$f(L) = \sum_{x \in a} \lfloor x / L \rfloor$$
Notice that $f(L)$ is a monotonically non-increasing function of $L$. That is, as $L$ increases, $f(L)$ stays the same or decreases. Because of this monotonic property:
- If a length $L$ produces at least $k$ segments ($f(L) \ge k$), then any length $L' < L$ will also produce at least $k$ segments.
- If $L$ produces fewer than $k$ segments, any $L' > L$ will also fail.
We can binary search for the maximum valid $L$ in the search space $[1, \max(a)]$:
- Handle edge cases: if $k \le 0$, return $\max(a)$ (or $0$ if
ais empty). - Set search range boundaries:
lo = 1,hi = max(a). - Compute
mid = (lo + hi) // 2. Calculatepieces = sum(x // mid for x in a). - If
pieces >= k,midis a valid length. Storebest = midand move the lower boundlo = mid + 1to search for larger valid lengths. - Otherwise,
midis too large. Move the upper boundhi = mid - 1.
Complexity
- Time Complexity: $O(n \log M)$, where $n$ is the length of
aand $M = \max(a)$. The binary search takes $O(\log M)$ iterations, and each iteration sums over $n$ elements. - Space Complexity: $O(1)$ auxiliary space.
from typing import List
def max_wood_length(a: List[int], k: int) -> int:
if k <= 0:
return max(a, default=0)
lo, hi = 1, max(a, default=0)
best = 0
while lo <= hi:
mid = (lo + hi) // 2
pieces = sum(length // mid for length in a)
if pieces >= k:
best = mid
lo = mid + 1
else:
hi = mid - 1
return best