-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllocate_Books.cpp
More file actions
42 lines (42 loc) · 883 Bytes
/
Copy pathAllocate_Books.cpp
File metadata and controls
42 lines (42 loc) · 883 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
40
41
42
#include <bits/stdc++.h>
bool isValid(vector<int> &A, int B, int curr){
int n = A.size();
int students = 1;
int curr_pages = 0;
for(int i=0;i<n;i++){
if (curr_pages+A[i]>curr){
curr_pages = A[i];
students++;
if(students>B)
return false;
}
else{
curr_pages += A[i];
}
}
return true;
}
int max(int a, int b){
return a>b?a:b;
}
int Solution::books(vector<int> &A, int B) {
int n = A.size(),i;
if (n<B)
return -1;
long long int st = 0, en = 0;
for(i=0;i<n;i++){
en += A[i];
st = max(st,A[i]);
}
int ans = 0;
while (st<=en){
long long int mid = (st+en)/2;
if (isValid(A,B,mid)){
ans = mid;
en = mid-1;
}
else
st = mid+1;
}
return ans;
}