We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9366275 commit e50a5c1Copy full SHA for e50a5c1
Python/divide-chocolate.py
@@ -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