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 the maximum length of the small pieces.
Example
For L=[232, 124, 456], k=7, return 114.
Note
You couldn’t cut wood into float length.
Challenge
O(n log Len), where Len is the longest length of the wood.
classSolution:""" @param L: Given n pieces of wood with length L[i] @param k: An integer return: The maximum length of the small pieces. """defwoodCut(self,L,k):# write your code here""" L: The maximum length of the small pieces (0<L<max(L[i])) if sum(L[i]/L) >= k, then L is the maximum length we want This problems is to find propoer L from range(0,max(L[i])), which can satisfy the above condition """# length > piecesifsum(L)<k:return0max_L=max(L)sorted_L=sorted(L)front=0end=max_Lwhilefront<=end:mid=(front+end)/2sum_pieces=sum([each_L/midforeach_Linsorted_L])# case 1: mid is too large to make k piecesifsum_pieces<k:end=mid-1# case 2: mid is too small that make more than k pieceselifsum_pieces>k:front=mid+1# case 3: exact k pieces, but we know not if other mid can also make 7 pieceselse:front=mid+1# result should be either "front" or "end"ifsum([l/endforlinL])>=k:returnendelse:returnfront