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