Skip to content

Commit 7f24a2b

Browse files
authored
Create cutting-ribbons.py
1 parent 3ba1fed commit 7f24a2b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Python/cutting-ribbons.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(nlogr), r is sum(ribbons)/k
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def maxLength(self, ribbons, k):
6+
"""
7+
:type ribbons: List[int]
8+
:type k: int
9+
:rtype: int
10+
"""
11+
def check(ribbons, k, s):
12+
return reduce(lambda total,x: total+x//s, ribbons, 0) >= k
13+
14+
left, right = 1, sum(ribbons)//k
15+
while left <= right:
16+
mid = left + (right-left)//2
17+
if not check(ribbons, k, mid):
18+
right = mid-1
19+
else:
20+
left = mid+1
21+
return right

0 commit comments

Comments
 (0)