LintCode 木材加工

作者: HarperKoo | 来源:发表于2016-01-11 20:44 被阅读191次

    试试自己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
    
    

    相关文章

      网友评论

        本文标题:LintCode 木材加工

        本文链接:https://www.haomeiwen.com/subject/yhnqkttx.html