-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1231-Divide Chocolate.cpp
More file actions
39 lines (39 loc) · 881 Bytes
/
1231-Divide Chocolate.cpp
File metadata and controls
39 lines (39 loc) · 881 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
//Same as 410. Split Array Largest Sum difference of upper_bound
class Solution {
public:
int K;
bool isValid(vector<int>& sweetness,int target)
{
int sum=0,count=0;
for(int &i:sweetness)
{
sum+=i;
if(sum>target)
{
if(++count==K)
return true;
sum=0;
}
}
return false;
}
int maximizeSweetness(vector<int>& sweetness, int K)
{
long j=0,mid;
int i=1e6;
for(int &s:sweetness)
j+=s,i=min(i,s);
if(K==0)
return j;
this->K=K+1;
while(i<j) //Upper_bound
{
mid=(i+j)>>1;
if(isValid(sweetness,mid))
i=mid+1;
else
j=mid;
}
return i;
}
};