Given an array task of size N denoting amount of work to be done for each task, the problem is to find the minimum amount of work to be done on each day so that all the tasks can be completed in at most D days.
Note: On one day work can be done for only one task.
Input: task = [3, 4, 7, 15], D = 10 Output: 4 Explanation: Here minimum work to be done is 4. On 1st day, task = 3 < 4 so this task can only be completed. Because work can be done for only one task on one day. For task = 4 the task can be completed in 1 day. Task = 7. Now 4 amount of work can be done in 1 day so to complete this task 2 days are required. Task = 15, 4 additional days are required. Total number of days required = 1 + 1 + 2 + 4 = 8 days < D. So 4 value would be the minimum value.
Approach: The problem can be solved using the binary search approach using the following idea:
The minimum work that can be done each day is 1 and the maximum work that can be done is the maximum of the tasks array. Search on this range and if the mid-value satisfies the condition then move to 1st half of the range else to the second half.
Follow the steps mentioned to implement the approach:
Create a variable left = 1, represent the starting point of the range, variable right = INT_MIN.
Run a loop from index = 0 to index = N – 1 and update right = max(right, task[index]).
Create a variable per_day_task = 0, to store the answer.
Run a while condition with left <= right
create a variable mid = left + (right – left)/2.
if all the task can be done within D days by doing mid amount of work on each day update per_day_task = mid and make right = mid – 1.
else right = mid + 1.
Print per_day_task as the minimum number of tasks done each day to complete all the tasks.
Below is the implementation for the above approach:
Please Login to comment...