# 二分搜索-木材切割问题

```python
class Solution:
    """
    @param L: Given n pieces of wood with length L[i]
    @param k: An integer
    return: The maximum length of the small pieces.
    最少切k块木材，切希望材料越长越好
    """

    def woodCut(self, L, k):
        if sum(L) < k:
            return 0

        start, end = 1, max(L)
        i = 0
        while start + 1 < end:
            i += 1
            # 木材长,这几约定为整数
            mid = int((start + end) / 2)
            print(i, mid)
            # 总共的木材
            pieces_sum = sum(int(len_i / mid) for len_i in L)
            # 比k少，说明木材长度取大了，所以end=mid进行减少逼近
            if pieces_sum < k:
                end = mid
            else:
                start = mid
                # 最少k块木头
        if sum(int(len_i / end) for len_i in L) >= k:
            return end
        return start


s = Solution()
print(s.woodCut([232, 124, 456], k=7))
'''
1 228
2 114
3 171
4 142
5 128
6 121
7 117
8 115
114
'''
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/sou-suo-cha-zhao-lei/mu-cai-qie-ge-wen-ti.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
