Minimum work to be done per day to finish given tasks within D days

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.
Examples:
Input: task[] = [3, 4, 7, 15], Â D = 10
Output: 4
Explanation: Â Here minimum work to be done is 4.
On 1st day, task[0] = 3 < 4 so this task can only be completed. Because work can be done for only one task on one day.
For task[1] = 4 the task can be completed in 1 day.
Task[2] = 7. Now 4 amount of work can be done in 1 day so to complete this task 2 days are required.
Task[3] = 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.Input: task[] = [30, 20, 22, 4, 21], D = 6
Output: 22
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:
C++
// C++ code to implement the approach#include <bits/stdc++.h>using namespace std;Â
// Function to check if// all the task can be// completed by 'per_day'// number of task per daybool valid(int per_day,           vector<int> task, int d){Â
    // Variable to store days required    // to done all tasks    int cur_day = 0;    for (int index = 0; index < task.size();         index++) {Â
        int day_req            = ceil((double)(task[index])                   / (double)(per_day));Â
        cur_day += day_req;Â
        // If more days required        // than 'd' days so invalidÂ
        if (cur_day > d) {            return false;        }    }Â
    // Valid if days are less    // than or equal to 'd'    return cur_day <= d;}Â
// Function to find minimum// task done each dayint minimumTask(vector<int> task, int d){Â
    int left = 1;    int right = INT_MAX;Â
    for (int index = 0;         index < task.size();         index++) {        right = max(right, task[index]);    }Â
    // Variable to store answer    int per_day_task = 0;Â
    while (left <= right) {Â
        int mid = left                  + (right - left) / 2;Â
        // If 'mid' number of task per day        // is valid so store as answer and        // more to first half        if (valid(mid, task, d)) {            per_day_task = mid;            right = mid - 1;        }        else {            left = mid + 1;        }    }Â
    // Print answer    return per_day_task;}Â
// Driver Codeint main(){    // Input taken    vector<int> task{ 3, 4, 7, 15 };    int D = 10;Â
    cout << minimumTask(task, D) << endl;Â
    return 0;} |
Java
// JAVA code to implement the approachimport java.util.*;class GFG {Â
  // Function to check if  // all the task can be  // completed by 'per_day'  // number of task per day  public static boolean    valid(int per_day, ArrayList<Integer> task, int d)  {Â
    // Variable to store days required    // to done all tasks    int cur_day = 0;    for (int index = 0; index < task.size(); index++) {Â
      double day_req        = (Math.ceil((double)task.get(index)                     / (double)(per_day)));Â
      cur_day += day_req;Â
      // If more days required      // than 'd' days so invalidÂ
      if (cur_day > d) {        return false;      }    }Â
    // Valid if days are less    // than or equal to 'd'    return cur_day <= d;  }Â
  // Function to find minimum  // task done each day  public static int minimumTask(ArrayList<Integer> task,                                int d)  {Â
    int left = 1;    int right = Integer.MAX_VALUE;Â
    for (int index = 0; index < task.size(); index++) {      right = Math.max(right, task.get(index));    }Â
    // Variable to store answer    int per_day_task = 0;Â
    while (left <= right) {Â
      int mid = left + (right - left) / 2;Â
      // If 'mid' number of task per day      // is valid so store as answer and      // more to first half      if (valid(mid, task, d)) {        per_day_task = mid;        right = mid - 1;      }      else {        left = mid + 1;      }    }Â
    // Print answer    return per_day_task;  }Â
  // Driver Code  public static void main(String[] args)  {    // Input taken    ArrayList<Integer> task = new ArrayList<Integer>(      Arrays.asList(3, 4, 7, 15));    int D = 10;Â
    System.out.println(minimumTask(task, D));  }}Â
// This code is contributed by Taranpreet |
Python
# Python code to implement the approachimport mathÂ
# Function to check if# all the task can be# completed by 'per_day'# number of task per daydef valid(per_day, task, d):Â
    # Variable to store days required    # to done all tasks    cur_day = 0    for index in range(0, len(task)):Â
        day_req = math.ceil((task[index]) / (per_day))Â
        cur_day += day_reqÂ
        # If more days required        # than 'd' days so invalid        if (cur_day > d):            return FalseÂ
    # Valid if days are less    # than or equal to 'd'    return cur_day <= dÂ
# Function to find minimum# task done each daydef minimumTask(task, d):Â
    left = 1    right = 1e9 + 7Â
    for index in range(0, len(task)):        right = max(right, task[index])Â
    # Variable to store answer    per_day_task = 0Â
    while (left <= right):Â
        mid = left + (right - left) // 2Â
        # If 'mid' number of task per day        # is valid so store as answer and        # more to first half        if (valid(mid, task, d)):            per_day_task = mid            right = mid - 1Â
        else:            left = mid + 1Â
    # Print answer    return math.trunc(per_day_task)Â
# Driver Code# Input takentask = [3, 4, 7, 15]D = 10Â
print(minimumTask(task, D))Â
# This code is contributed by Samim Hossain Mondal. |
C#
// C# program to implement// the above approachusing System;using System.Collections.Generic;Â
public class GFG{Â
  // Function to check if  // all the task can be  // completed by 'per_day'  // number of task per day  public static bool    valid(int per_day, List<int> task, int d)  {Â
    // Variable to store days required    // to done all tasks    int cur_day = 0;    for (int index = 0; index < task.Count; index++) {Â
      double day_req        = (Math.Ceiling((double)task[(index)]                        / (double)(per_day)));Â
      cur_day += (int)day_req;Â
      // If more days required      // than 'd' days so invalidÂ
      if (cur_day > d) {        return false;      }    }Â
    // Valid if days are less    // than or equal to 'd'    return cur_day <= d;  }Â
  // Function to find minimum  // task done each day  public static int minimumTask(List<int> task,                                int d)  {Â
    int left = 1;    int right = Int32.MaxValue;Â
    for (int index = 0; index < task.Count; index++) {      right = Math.Max(right, task[index]);    }Â
    // Variable to store answer    int per_day_task = 0;Â
    while (left <= right) {Â
      int mid = left + (right - left) / 2;Â
      // If 'mid' number of task per day      // is valid so store as answer and      // more to first half      if (valid(mid, task, d)) {        per_day_task = mid;        right = mid - 1;      }      else {        left = mid + 1;      }    }Â
    // Print answer    return per_day_task;  }Â
  // Driver Code  public static void Main(String []args)  {         // Input taken    List<int> task = new List<int>();    task.Add(3);    task.Add(4);    task.Add(7);    task.Add(15);    int D = 10;Â
    Console.WriteLine(minimumTask(task, D));  }}Â
// This code is contributed by code_hunt. |
Javascript
<script>Â Â Â Â Â Â Â // JavaScript code for the above approachÂ
       // Function to check if       // all the task can be       // completed by 'per_day'       // number of task per day       function valid(per_day,           task, d) {Â
           // Variable to store days required           // to done all tasks           let cur_day = 0;           for (let index = 0; index < task.length;               index++) {Â
               let day_req                   = Math.ceil((task[index])                       / (per_day));Â
               cur_day += day_req;Â
               // If more days required               // than 'd' days so invalidÂ
               if (cur_day > d) {                   return false;               }           }Â
           // Valid if days are less           // than or equal to 'd'           return cur_day <= d;       }Â
       // Function to find minimum       // task done each day       function minimumTask(task, d) {Â
           let left = 1;           let right = Number.MAX_VALUE;Â
           for (let index = 0;               index < task.length;               index++) {               right = Math.max(right, task[index]);           }Â
           // Variable to store answer           let per_day_task = 0;Â
           while (left <= right) {Â
               let mid = left                   + (right - left) / 2;Â
               // If 'mid' number of task per day               // is valid so store as answer and               // more to first half               if (valid(mid, task, d)) {                   per_day_task = mid;                   right = mid - 1;               }               else {                   left = mid + 1;               }           }Â
           // Print answer           return per_day_task;       }Â
       // Driver CodeÂ
       // Input taken       let task = [3, 4, 7, 15];       let D = 10;Â
       document.write(minimumTask(task, D) + '<br>');Â
    // This code is contributed by Potta Lokesh   </script> |
Â
Â
4
Â
Time Complexity: O(N * logN)
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



