Wood Cut
Example
Solution
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):
# very important here, we are supposed the min = 1 not 0.1 or deciaml value
if sum(L) < k:
return 0
start, end = 1, max(L)
while start + 1 < end:
mid = ( start + end ) / 2
pieces = sum( l / mid for l in L )
if pieces >= k :
start = mid
else:
end = mid
# why is greater and equal k instead of == k, because it may cannot cut right into k piece
if sum(l / end for l in L) >= k:
return end
return startLast updated