-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
39 lines (38 loc) · 851 Bytes
/
Copy pathSolution.java
File metadata and controls
39 lines (38 loc) · 851 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
29
30
31
32
33
34
35
36
37
38
39
class Solution
{
public int maximizeSweetness(int[] sweetness, int K)
{
int total = 0;
int min = Integer.MAX_VALUE;
for (int sweet : sweetness)
{
total += sweet;
min = Math.min(min, sweet);
}
int l = min, r = total;
while (l < r)
{
int mid = l + (r - l) / 2;
int groups = 0;
int current = 0;
for (int sweet : sweetness)
{
current += sweet;
if (current > mid)
{
current = 0;
groups++;
}
}
if (groups > K)
{
l = mid + 1;
}
else
{
r = mid;
}
}
return l;
}
}