LintCode 木材加工

试试自己python学的怎么样,这题实在太简单了哈哈。

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

import math
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):
        allLong=0
        for l in L:
            allLong = allLong+l
        maxLen=allLong/k
        #print "maxLen",maxLen
        if maxLen == 0:
            return 0
        low=0
        high=maxLen
        while True:
            middle=(low+high)/2
            if (low+high) == 1:
                middle = 1
            #print "low",low
            #print "high",high
            #print "middle",middle
            count=0
            for l in L:
                count = count+l/middle
            if low == middle:
                return middle
            if count >= k:
                low = middle
                continue

            if count < k:
                high = middle
                continue
s=Solution()
L=[232, 124, 456]
length = s.woodCut(L,7)
#print length

    原文作者:HarperKoo
    原文地址: https://www.jianshu.com/p/1886f551b03f#comments
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞