Skip to content

Commit e277837

Browse files
authored
Merge pull request #78 from arpitjain22june/patch-10
Create PainterProblem.java
2 parents 2398642 + 34e7a69 commit e277837

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.Scanner;
2+
3+
public class PainterProblem {
4+
//find the minimum time to paint n board with different length and 1 unit length take 1 unit time by k painter
5+
// painter can only paint continous block
6+
// 1 painter will paint 1 block only
7+
public static int pno(int[] a, int n, long mid) {
8+
long s = 0;
9+
int p = 1;
10+
for (int i = 0; i < n; i++) {
11+
s += a[i];
12+
if (s > mid) {
13+
s = a[i];
14+
p++;
15+
}
16+
}
17+
return p;
18+
}
19+
20+
public static void main(String[] args) {
21+
Scanner sc = new Scanner(System.in);
22+
int k = -1;
23+
if (sc.hasNext()) {
24+
k = sc.nextInt();
25+
}
26+
int n = sc.nextInt();
27+
int[] a = new int[n];
28+
long s = 0;//sum
29+
int m = 0;//max
30+
for (int i = 0; i < n; i++) {
31+
a[i] = sc.nextInt();
32+
s += a[i];
33+
m = Math.max(m, a[i]);
34+
}
35+
36+
long l = m;//lowest time = max block length in array
37+
long h = s;//maximum time = sum of all board length
38+
while (l < h) {
39+
long mid = (l + h) / 2;
40+
if (pno(a, n, mid) <= k) {//if it is possible to paint in this time
41+
h = mid;//try to search for more lower value therefore dec h
42+
// we cant do h= mid -1 as we need a answer and mid was possible
43+
} else {
44+
l = mid + 1;//it wasn't possible then search in upper half and inc l
45+
}
46+
}
47+
System.out.println(l);
48+
49+
}
50+
51+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Given K painters to paint N boards where each painter takes 1 unit of time to paint 1 unit of boards i.e. if the length of a particular board is 5, it will take 5 units of time to paint the board. Compute the minimum amount of time to paint all the boards.
2+
3+
Note that:
4+
5+
Every painter can paint only contiguous segments of boards.
6+
A board can only be painted by 1 painter at maximum.
7+
Input Format
8+
First line contains K which is the number of painters. Second line contains N which indicates the number of boards. Third line contains N space separated integers representing the length of each board.
9+
10+
Constraints
11+
1 <= K <= 10
12+
1 <= N <= 10
13+
1<= Length of each Board <= 10^8
14+
15+
Output Format
16+
Output the minimum time required to paint the board.
17+
18+
Sample Input
19+
2
20+
2
21+
1 10
22+
Sample Output
23+
10

0 commit comments

Comments
 (0)