-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaintersPartition.java
More file actions
68 lines (55 loc) · 1.46 KB
/
PaintersPartition.java
File metadata and controls
68 lines (55 loc) · 1.46 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class PaintersPartition {
// User function Template for Java
class Solution {
public int minTime(int[]C, int A) {
// code here
int n=C.length;
long l=0;
long h=0;
//min //max
for(int i=0;i<n;i++)
{
l=Math.max(l,C[i]); //sbsay ,max element from array
h+=C[i]; // summation of all element
}
long ans=h; //want min- therefore initialise max
while(l<=h)
{
long mid=l+(h-l)/2;
if(check(C,A,mid))
{
//if true
ans=(mid) ;
//go left for optimal
h=mid-1;
}
else
{
l=mid+1;
}
}
return (int)ans;
}
//check function
public static boolean check(int[]arr, int worker,long mid)
{
int sum=0;
int painter=1;
int n=arr.length;
for(int i=0;i<n;i++)
{
sum+=arr[i];
if(sum>mid)
{
painter++; //assign new painters
sum=arr[i];
}
if(painter>worker)
{
return false;
}
}
return true;
}
}
}