forked from yingl/LintCodeInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwood_cut.py
More file actions
28 lines (26 loc) · 717 Bytes
/
wood_cut.py
File metadata and controls
28 lines (26 loc) · 717 Bytes
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
# -*- coding: utf-8 -*-
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 (not L) or (k == 0):
return 0
L.sort()
low, up = 1, L[-1]
while low <= up:
mid = low + (up - low) / 2
if self.calc(L, mid) >= k:
low = mid + 1
else:
up = mid - 1
return up
# 计算根据wood_len,总共可以切几段。
def calc(self, L, wood_len):
ret = 0
for l in L:
ret += l / wood_len
return ret