Skip to content

Commit e50a5c1

Browse files
authored
Create divide-chocolate.py
1 parent 9366275 commit e50a5c1

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Python/divide-chocolate.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Time: O(nlogn)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def maximizeSweetness(self, sweetness, K):
6+
"""
7+
:type sweetness: List[int]
8+
:type K: int
9+
:rtype: int
10+
"""
11+
def check(sweetness, K, x):
12+
curr, cuts = 0, 0
13+
for s in sweetness:
14+
curr += s
15+
if curr >= x:
16+
cuts += 1
17+
curr = 0
18+
return cuts >= K+1
19+
20+
left, right = min(sweetness), sum(sweetness)//(K+1)
21+
while left <= right:
22+
mid = left + (right-left)//2
23+
if not check(sweetness, K, mid):
24+
right = mid-1
25+
else:
26+
left = mid+1
27+
return right

0 commit comments

Comments
 (0)