forked from Anisha2001/CP_practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllocate Minimum Number of Pages.cpp
65 lines (57 loc) · 1.58 KB
/
Allocate Minimum Number of Pages.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
/*
Given number of pages in n different books and m students. The books are arranged in ascending order of number of pages. Every student is assigned to read some consecutive books. The task is to assign books in such a way that the maximum number of pages assigned to a student is minimum.
Example :
Input : pages[] = {12, 34, 67, 90}
m = 2
Output : 113
Explanation:
There are 2 number of students. Books can be distributed
in following fashion :
1) [12] and [34, 67, 90]
Max number of pages is allocated to student
2 with 34 + 67 + 90 = 191 pages
2) [12, 34] and [67, 90]
Max number of pages is allocated to student
2 with 67 + 90 = 157 pages
3) [12, 34, 67] and [90]
Max number of pages is allocated to student
1 with 12 + 34 + 67 = 113 pages
Of the 3 cases, Option 3 has the minimum pages = 113.
*/
bool isValid(int arr[], int n, int m, int max) {
int student = 1; // 1st student
int sum = 0; // total pages student have
for(int i = 0; i < n; i++) {
sum += arr[i];
if(sum > max) {
student++;
sum = arr[i];
}
if(student > m)
return false;
}
return true;
}
int findPages(int arr[], int n, int m)
{
if(n < m) return -1;
int start = -1; // max
int end = 0; // total
for(int i = 0; i < n; i++) {
start = max(arr[i],start);
end += arr[i];
}
int ans = -1;
int mid;
while(start <= end) { // binary search
mid = start + (end - start) / 2;
if(isValid(arr, n, m, mid)) {
ans = mid;
end = mid - 1;
}
else {
start = mid + 1;
}
}
return ans;
}