-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkSegments.cpp
68 lines (49 loc) · 1.08 KB
/
kSegments.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<bits/stdc++.h>
using namespace std;
/*
Divide an array into k segments to
maximize maximum of segment minimums
*/
bool isPossible(vector<int> &vect,int m,int currMin){
int stuReq = 1;
int curSum = 0;
for(int i=0;i<vect.size();i++){
if(vect[i] > currMin)
return false;
if(curSum + vect[i] > currMin){
stuReq++;
curSum = vect[i];
if(stuReq > m)
return false;
}else{
curSum += vect[i];
}
}
return true;
}
int books(vector<int> &vect,int m){
int sum = 0;
int cnt = 0;
int n = vect.size();
if( n < m)
return -1;
for(int i=0;i<n;i++)
sum += vect[i];
int low = 0,high = sum;
int ans = INT_MAX;
while(low <= high){
int mid = low + (high - low)/2;
if(isPossible(vect,m,mid)){
ans = min(ans,mid);
high = mid-1;
}
else
low = mid + 1;
}
return ans;
}
int main(){
vector<int> vect{10,3,5,7};
cout<<books(vect,2)<<endl;
return 0;
}