


Input: arr = [2, 1, 5, 6, 2, 3], k = 2
Output: 11
Explanation:
First painter can paint boards 1 to 3 in 8 units of time and the second painter can paint boards 4-6 in 11 units of time. Thus both painters will paint all the boards in max(8,11) = 11 units of time. It can be shown that all the boards can't be painted in less than 11 units of time.
The first line contains two integers ‘n’ and ‘k’ denoting the number of elements in the array/list and number of painters available.
The second line contains ‘n’ single space-separated integers denoting the elements of the array/list.
Return the minimum time required to get the job done.
You do not need to print anything; it has already been taken care of.
All we need to do is to Divide the boards into k of fewer partitions such that the maximum sum of the elements in a partition, overall partitions is minimized.
We can put the (k - 1)th divider between the ith and (i + 1)th element where i is between 1 and N.
Now the recursive function will just require two conditions that can lead to the result and that will be out the maximum total cost:
Now to avoid the exponential time complexity we can use Dynamic Programming since it already keeps a track of pre-calculated data and will lead to better results if we can take the data from the dp table.
Consider we have N painters, then painters with index ‘i’ can paint fences only in continuous order. So, this means the first painter will paint some of the fences. Then the second painter paints some of the fences. The same goes for all the painters. This may happen that some painter does not even get a fence to paint.
How can we improvise our solution, we can use binary search for that.