[Lintcode]183. Wood Cut
183. Wood Cut
Example Challenge Notice If you couldn‘t get >= k pieces,return 0. 我的代码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):
# write your code here
# write your code here
if L == []:
return 0
L.sort()
max = L[-1]
l = 1
r = L[-1]
while(l<=r):
count = 0
m = (l + r) // 2
print(l,r,m)
for i in L:
count += i//m
if count>=k:
l = m+1
else:
r = m-1
#1.边界问题,最后结果可能是从右向左逼近,此时m为第一个不满足条件的情况,反之,从左往右逼近,则m为满足条件的情况
print(m)
count = 0
for i in L:
count += i//m
print(count)
if count >= k:
return m
else:
return m-1
思路
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
