forked from ShivamDubey7/Competitive-Programming-Algos
-
Notifications
You must be signed in to change notification settings - Fork 315
/
Copy pathAllocateMinNoOfPages
42 lines (40 loc) · 967 Bytes
/
AllocateMinNoOfPages
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
class AllocateMinNoOfPages
{
public static int findPages(int[]A,int N,int M)
{
int sum = 0;
int maxNum = 0;
for(int i = 0;i<N;i++){
sum+=A[i];
maxNum = Math.max(maxNum,A[i]);
}
int low = maxNum;
int high = sum;
int res = -1;
while(low<=high){
int mid = low+(high-low)/2;
if(isValid(A,N,M,mid)){
res = mid;
high = mid-1;
}else{
low = mid+1;
}
}
return res;
}
public static boolean isValid(int[] A,int N,int M,int max){
int student = 1;
int s = 0;
for(int i = 0;i<N;i++){
s+=A[i];
if(s>max){
student++;
s = A[i];
}
if(student>M){
return false;
}
}
return true;
}
};