-
Notifications
You must be signed in to change notification settings - Fork 1k
/
painter_partition.c
66 lines (57 loc) · 1.41 KB
/
painter_partition.c
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
#include <stdio.h>
int painters(int * arr, int maxtime, int len, int num) {
int time = 0, count = 1;
for (int i = 0; i < len; i++) {
time += arr[i];
if (time > maxtime) {
time = arr[i];
count++;
if (count > num) {
return 0;
}
}
}
return 1;
}
int get_max(int arr[], int len) {
int max = arr[0];
for (int i = 1; i < len; i++) {
if (max < arr[i]) max = arr[i];
}
return max;
}
int main() {
int len, num, time;
int sum = 0;
printf("Enter the length of array, num of painters and time: ");
scanf("%d%d%d", & len, & num, & time);
int arr[len];
printf("Enter the array: ");
for (int i = 0; i < len; i++) {
scanf("%d", & arr[i]);
sum += arr[i];
}
// Finding the max of array
int start = get_max(arr, len);
int end = sum, ans;
while (start <= end) {
int mid = (start + end) / 2;
if (painters(arr, mid, len, num)) {
ans = mid;
end = mid - 1;
} else {
start = mid + 1;
}
}
ans = ans * time;
printf("Total time taken: %d", ans);
return 0;
}
/*
Output:
Enter the length of array,num of painters and time: 2 2 5
Enter the array: 1 10
Total time taken: 50
Time complexity : O(n*log(N)) (where N is sum of array elements)
space complexity : O(n) (n is size of array)
*/