-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathAllocate minimum number of pages(Java)
60 lines (52 loc) · 1.08 KB
/
Allocate minimum number of pages(Java)
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
class Solution
{
//Function to find minimum number of pages.
public static int findPages(int[]a, int n, int m)
{
//Your code here
int min = maxOf(a);
int max = sumOf(a);
int res = 0;
while(min<=max) {
int mid = (min + max)/2;
if(isfeasible(a, m, mid)) {
res = mid;
max = mid -1;
}else{
min = mid + 1;
}
}
return res;
}
static boolean isfeasible(int[]a, int m, int res) {
int student = 1, sum = 0;
for(int i =0; i<a.length; i++){
if(sum + a[i] > res){
student++;
sum = a[i];
}else{
sum += a[i];
}
}
return student <= m;
}
static int maxOf(int a[] ) {
int max = a[0];
for(int i =1; i<a.length; i++){
if(a[i]>max){
max = a[i];
}
}
return max;
}
static int sumOf(int a[]) {
int s = 0;
for(int i = 0; i<a.length; i++){
s = s + a[i];
}
return s;
}
}
© 2021 GitHub, Inc.
Terms
Privacy