-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaintersPartition.cpp
55 lines (55 loc) · 1.09 KB
/
PaintersPartition.cpp
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
#include<iostream>
#include<vector>
using namespace std;
bool possibleSol(vector <int> &arr,int k,int mid)
{
int PainterCount = 1;
int SumTime = 0;
for (int i=0;i<arr.size();i++)
{
if (SumTime + arr[i] <= mid)
{
SumTime = SumTime + arr[i];
}
else
{
PainterCount++;
if(PainterCount > k || arr[i] > mid)
{
return false;
}
SumTime = 0;
SumTime = SumTime + arr[i];
}
}
return true;
}
int main()
{
vector <int> arr = {5, 5, 5, 5}; // Number of unit Boards
int k = 2; // Number Of Painters
int start = 0;
int end;
int ans = -1;
int sum = 0;
for (int i=0;i<arr.size();i++)
{
sum = sum + arr[i];
}
end = sum;
int mid = (start + end)/2;
while (start<=end)
{
if(possibleSol(arr, k, mid))
{
ans = mid;
end = mid - 1;
}
else
{
start = mid + 1;
}
mid = start + (end - start)/2;
}
cout<<ans;
}