Wood Cut

http://www.lintcode.com/zh-cn/problem/wood-cut/

有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目至少为 k。当然,我们希望得到的小段越长越好,你需要计算能够得到的小段木头的最大长度。

注意事项

木头长度的单位是厘米。原木的长度都是正整数,我们要求切割得到的小段木头的长度也要求是整数。无法切出要求至少 k 段的,则返回 0 即可。

Lintcode上面的一道题目,很有意思,光看题可能想象不出是用二分来做。但是实际小段的长度可以使用二分来确定。left为0,right为原始木头的最长长度(注意不是最小长度)。但是这道题二分的复杂度也不是O(logn)而是和木头的数目也成正比关系(每次对于划分的长度,需要计算一遍所有的木头得到可以锯出的小段数目)。具体代码如下:

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):
        if not L:
            return 0

        l = 0
        r = max(L)

        while l+1 < r:
           mid = l + (r-l)/2
           if  sum([i/mid  for i in L]) >= k:
               l = mid
           else:
               r = mid
        if sum([i/r for i in L]) < k:
            return l
        else:
            return r 
时间: 2024-10-11 09:26:19

Wood Cut的相关文章

[Lintcode]183. Wood Cut

183. Wood Cut 本题难度: Hard Topic: Binary Search Description 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

Lintcode: 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 th

Bug Free

1 Search Insert Position---NOT BUG FREE 1 Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. 2 3 You may assume no duplicates in the array. 注意插入位置

Binary Search 二分查找总结

Binary Search基本的复杂度为O(logn).如果提示需要对O(n)的算法进行优化,非常可能就是二分,另外二分一般出现在排序数组或者变形后的排序数组(rotated array)当中.二分主要有两种,binary search on index(index上的二分)和binary search on result(结果上的二分).index上的二分主要有 result上的二分主要有Sqrt(x),Wood cut两种. 另外binary search的版本很多,区别在终结条件,比如是l

CS3K.com 九章算法强化班

Advanced Data Structure -- Union Find Number of Islands 题目 思路I:BFS 避免相同位置的元素重复入列,访问过的元素都要标记为已访问.BFS为了得到下一个点的坐标,所以需要建立一个表示位置的坐标类. 思路II:并查集 等价于求集合中连通块的个数. Number of Islands II 题目 思路:并查集,把二维数组转化为一维father数组. LeetCode 547. Friend Circles 题目 思路:并查集的典型应用.题目

刷题--二分法(4)

最后一种二分法的可能情况:最终的答案是二分的.也就是说答案是处在sort array中的,需要每次验证一下mid对应的值是偏大还是偏小. 例  lintcode 183. Wood Cut   https://www.lintcode.com/problem/wood-cut/description 从考虑答案的角度,切割长度从1开始增加,长度为1的时候可能切出几百根,为2时几百根,随着长度增加,能切出的根数逐渐减小,直到到达最小的 大于等于k 的根数,即找对应根数大于等于k的最后一个位置,所以

red oak plywood- plain sliced and rotary cut myav

<img title="red oak plywood- plain sliced and rotary cut" alt="red oak plywood- plain sliced and rotary cut" src="http://s12.sinaimg.cn/bmiddle/507a9ec8g70eb53f7624b&690" real_src="http://s12.sinaimg.cn/bmiddle/50

HDU5203 Rikka with wood sticks

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta have a wood stick of length n which consists of n linked sticks of length 1. So it

hdu 5203 Rikka with wood sticks(Bestcoder Round #37)

Rikka with wood sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 446    Accepted Submission(s): 130 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situ