Quick Overview

Find the largest integer segment length that can produce at least k pieces from the given wood lengths. Apply binary search to the monotone feasibility test and return zero when even unit-length segments are insufficient.

Maximize the Equal Wood Segment Length

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

# Maximize the Equal Wood Segment Length ## Problem You have several pieces of wood with positive integer lengths. A piece may be cut into shorter segments, but material from different pieces cannot be joined. Leftover material may be discarded. Return the largest positive integer length `L` for which the wood can produce at least `k` segments of length `L`. Return `0` if even length `1` cannot produce `k` segments. ### Function Contract Implement `maxSegmentLength(woods, k)`. - Input: a list of positive integer wood lengths and a positive integer `k`. - Output: the maximum feasible integer segment length, or `0` when none is feasible. ### Examples ```text Input: woods = [9, 7, 5], k = 5 Output: 3 Explanation: lengths 9, 7, and 5 produce 3, 2, and 1 segments of length 3. ``` ```text Input: woods = [2, 3], k = 6 Output: 0 ``` ### Clarifications - Cutting has no loss beyond the discarded remainder. - The answer cannot exceed the longest original piece because segments cannot be assembled from multiple pieces. - The segment count for a candidate length may be larger than `k`; only feasibility matters. ```hint Test a proposed length For a fixed positive length, count how many whole segments each piece contributes and decide whether the total reaches `k`. ``` ```hint Look for monotonicity If one candidate length is feasible, determine what that implies about every smaller positive candidate. ```

Quick Answer: Find the largest integer segment length that can produce at least k pieces from the given wood lengths. Apply binary search to the monotone feasibility test and return zero when even unit-length segments are insufficient.

Given positive integer wood lengths and a positive target k, return the largest positive integer segment length that yields at least k pieces without joining material. Return 0 if unit segments are insufficient.

Constraints

  • 1 <= woods.length <= 5,000.
  • 1 <= woods[i] <= 5 * 10^12.
  • 1 <= k <= 10^12.
  • Only whole integer-length segments count, and leftover material may be discarded.

Examples

Input: ([9, 7, 5], 5)

Expected Output: 3

Explanation: Length three yields enough pieces while length four does not.

Input: ([2, 3], 6)

Expected Output: 0

Explanation: Even unit segments cannot reach the requested count.

Hints

  1. For a proposed length, sum how many whole segments each original piece contributes.
  2. Feasibility changes monotonically as the proposed segment length increases.

Loading coding console...