-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopy Books.cpp
More file actions
33 lines (33 loc) · 825 Bytes
/
Copy Books.cpp
File metadata and controls
33 lines (33 loc) · 825 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
class Solution {
public:
/**
* @param pages: a vector of integers
* @param k: an integer
* @return: an integer
*/
bool __copyBooks(vector<int> &pages, int k, int m) {
int sum = 0, count = 1;
for(auto& a:pages) {
sum += a;
if(sum > m) {
count++;
if(count > k) return false;
sum = a;
if(sum > m) return false;
}
}
return true;
}
int copyBooks(vector<int> &pages, int k) {
// write your code here
if(pages.size() == 0) return 0;
int l = 0, r = INT_MAX;
while(l < r) {
int m = l + (r-l)/2;
if(__copyBooks(pages, k, m))
r = m;
else l = m+1;
}
return l;
}
};