[Lintcode] 183. Wood Cut

183. Wood Cut (题目链接)

Given n pieces of wood with length L[i] (integer array). Cut them into small pieces to guarantee you could have equal or more than k pieces with the same length. What is the longest length you can get from the n pieces of wood? Given L & k, return the maximum length of the small pieces.

Example

Example 1

Input:
L = [232, 124, 456]
k = 7
Output: 114
Explanation: We can cut it into 7 pieces if any piece is 114cm long, however we can't cut it into 7 pieces if any piece is 115cm long.

Example 2

Input:
L = [1, 2, 3]
k = 7
Output: 0
Explanation: It is obvious we can't make it.

Challenge

O(n log Len), where Len is the longest length of the wood.

Notice

You couldn't cut wood into float length.

If you couldn't get >= k pieces, return 0.

 

思路:

一开始直接暴力搜索,结果超时。看题解说是可以二分,那么怎么二分?

找到最长的木棍,对该木棍进行二分,如果分得的木棍个数符合要求,l = m + 1,否则r = m - 1

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
    """

    def woodCut(self, L, k):
        # write your code he
        if not L:
            return 0

        length = L[0]
        for wood in L[1:]:
            length = max(wood, length)
        l, r = 1, length
        rlt = 0
        while l <= r:
            m = (l + r) // 2
            cnt = 0
            for wood in L:
                temp = wood // m
                if temp > 0:
                    cnt += temp
            if cnt >= k:
                l = m + 1
                rlt = m
            else:
                r = m - 1
        return rlt

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 jQuery 实现的示例代码,可以根据上述要求设置 Cutout 选项和 Cutout 宽度尺寸限制: ```javascript $(document).ready(function() { // 获取 Wood Ultimate Cordless 和 MFF 的 Cutout 选项和 Cutout 宽度尺寸限制 var wood_ultimate_cordless = { min_width: 391, max_width_1: 493, max_width_2: 544, max_width_3: Infinity, min_cutout: 3, max_cutout_1: 52, max_cutout_2: 76, max_cutout_3: 127 }; var mff = { min_width: 381, max_width_1: 483, max_width_2: 534, max_width_3: Infinity, min_cutout: 3, max_cutout_1: 52, max_cutout_2: 76, max_cutout_3: 127 }; // 根据选择的产品设置相应的 Cutout 选项和 Cutout 宽度尺寸限制 $(".product-select").on("change", function() { var product = $(this).val(); var data = null; if (product === "Wood Ultimate Cordless") { data = wood_ultimate_cordless; } else if (product === "MFF") { data = mff; } // 根据选定的宽度范围设置相应的 Cutout 宽度尺寸限制 $(".width-select").on("change", function() { var width = parseInt($(this).val()); var max_cutout = null; if (width >= data.min_width && width <= data.max_width_1) { max_cutout = data.max_cutout_1; } else if (width > data.max_width_1 && width <= data.max_width_2) { max_cutout = data.max_cutout_2; } else if (width > data.max_width_2 && width <= data.max_width_3) { max_cutout = data.max_cutout_3; } // 设置 Cutout 宽度尺寸限制的提示文本 if (max_cutout) { $(".cutout-size-limit").text(data.min_cutout + "mm - " + max_cutout + "mm"); } else { $(".cutout-size-limit").text("N/A"); } }); }); }); ``` 该示例代码将 Wood Ultimate Cordless 和 MFF 的 Cutout 选项和 Cutout 宽度尺寸限制保存为对象,并根据选择的产品和宽度范围动态设置相应的 Cutout 宽度尺寸限制,并将其显示在页面上。请根据实际情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值