Wood Cut
ID; 183; hard; 木材加工
Last updated
ID; 183; hard; 木材加工
Last updated
There are two key requirements in this problem:
The woods need to be cut into k
pieces of the same length.
The length of the pieces should be maximized.
We use binary search to search for the cut length, i.e., the length of the pieces. The initial range for this length is [1, max]
where max
is the maximum length among the woods. We take the middle number and count the number of pieces using that number as the cut length.
If count ≥ k
, the first requirement is satisfied but we still need to test the second requirement, so we do not immediately return. Now the new range is [mid, max]
.
If count < k
, the first requirement is not satisfied, which means we are cutting too much. Then, the new range is [1, mid]
.
Lastly, we end up with the range [left, right]
. To satisfy the two requirements, we would return right
if possible since right
is larger than left
.