What can I do if k < n, I am not getting it can you please help
Please eleborate your Question So That i can help you more better.
Sir if no. of painters is lesser than the no. of boards then in that case what should be the logic, that’s what I am not getting in this problem
In that case we will return the sum which will left at last for example if my values are like
1 10 10
then my output will 10 will le assigned to painter 1 and left values sum 10 and 1 will be our output.
if you want to understand whole problem please read this:-
We know that the invariant of binary search has two main parts:
- the target value would always be in the searching range.
- the searching range will decrease in each loop so that the termination can be reached.
We also know that the values in this range must be in sorted order. Here our target value is the maximum sum of a contiguous section in the optimal allocation of boards. Now how can we apply binary search for this? We can fix the possible low to high range for the target value and narrow down our search to get the optimal allocation.
We can see that the highest possible value in this range is the sum of all the elementsin the array and this happens when we allot 1 painter all the sections of the board. The lowest possible value of this range is the maximum value of the array max, as in this allocation we can allot max to one painter and divide the other sections such that the cost of them is less than or equal to max and as close as possible to max. Now if we consider we use x painters in the above scenarios, it is obvious that as the value in the range increases, the value of x decreases and vice-versa. From this we can find the target value when x=k and use a helper function to find x, the minimum number of painters required when the maximum length of section a painter can paint is given.
I Hope your doubt will be clear now and if not Please let me know