forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplit Array Largest Sum.cpp
38 lines (38 loc) · 951 Bytes
/
Split Array Largest Sum.cpp
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
class Solution {
public:
int splitArray(vector<int>& nums, int m) {
long long low = 0;
long long res =0;
long long high = 1000000005;
while(low<=high){
long long mid = (low+high)/2;
int cnt = 1;
long long current_sum = 0;
int can = 1;
for(auto num: nums){
if(num > mid){
can = 0;
break;
}
if(current_sum+num>mid){
cnt ++;
current_sum = 0;
}
current_sum += num;
}
if(can==1){
if(cnt>m){
low = mid+1;
}
else{
res = mid;
high = mid-1;
}
}
else{
low = mid+1;
}
}
return res;
}
};