LintCode Q183 Wood Cut in Python

  • Jinhai ZHOU
  • 3 Minutes
  • 2017年1月1日
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
if len(L) == 0 or k == 0:
return 0
lo, hi = 1, max(L)
res = 0
while lo <= hi:
mid = lo + (hi - lo)/2
if self.countNumber(L, k, mid):
lo = mid + 1
res = mid
else:
hi = mid - 1
return res
def countNumber(self, L, k, length):
count = 0
for wood in L:
count += wood/length
if count >= k:
return True
return False
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。