-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion_11.cpp
More file actions
50 lines (47 loc) · 1.08 KB
/
question_11.cpp
File metadata and controls
50 lines (47 loc) · 1.08 KB
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
#include <iostream>
#include <climits>
using namespace std;
class Solution {
public:
int findPages(int A[], int N, int M) {
if (M > N) {
return -1;
}
int end = 0, start = INT_MIN, ans;
for (int i = 0; i < N; i++) {
start = max(A[i], start);
end += A[i];
}
int page = 0, count = 0, mid;
while (start <= end) {
mid = start + (end - start) / 2;
page = 0, count = 1;
for (int i = 0; i < N; i++) {
page = page + A[i];
if (page > mid) {
count++;
page = A[i];
}
}
if (count <= M) {
ans = mid;
end = mid - 1;
}
else {
start = mid + 1;
}
}
return ans;
}
};
int main() {
int t = 1;
while (t--) {
int N = 4;
int M = 2;
int A[] = {12, 34, 67, 90};
Solution ob;
cout << ob.findPages(A, N, M) << endl;
}
return 0;
}