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++
#include <bits/stdc++.h>
using namespace std;
bool valid( int per_day,
vector< int > task, int d)
{
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 (cur_day > d) {
return false ;
}
}
return cur_day <= d;
}
int 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]);
}
int per_day_task = 0;
while (left <= right) {
int mid = left
+ (right - left) / 2;
if (valid(mid, task, d)) {
per_day_task = mid;
right = mid - 1;
}
else {
left = mid + 1;
}
}
return per_day_task;
}
int main()
{
vector< int > task{ 3, 4, 7, 15 };
int D = 10;
cout << minimumTask(task, D) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static boolean
valid( int per_day, ArrayList<Integer> task, int d)
{
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 (cur_day > d) {
return false ;
}
}
return cur_day <= d;
}
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));
}
int per_day_task = 0 ;
while (left <= right) {
int mid = left + (right - left) / 2 ;
if (valid(mid, task, d)) {
per_day_task = mid;
right = mid - 1 ;
}
else {
left = mid + 1 ;
}
}
return per_day_task;
}
public static void main(String[] args)
{
ArrayList<Integer> task = new ArrayList<Integer>(
Arrays.asList( 3 , 4 , 7 , 15 ));
int D = 10 ;
System.out.println(minimumTask(task, D));
}
}
|
Python
import math
def valid(per_day, task, d):
cur_day = 0
for index in range ( 0 , len (task)):
day_req = math.ceil((task[index]) / (per_day))
cur_day + = day_req
if (cur_day > d):
return False
return cur_day < = d
def minimumTask(task, d):
left = 1
right = 1e9 + 7
for index in range ( 0 , len (task)):
right = max (right, task[index])
per_day_task = 0
while (left < = right):
mid = left + (right - left) / / 2
if (valid(mid, task, d)):
per_day_task = mid
right = mid - 1
else :
left = mid + 1
return math.trunc(per_day_task)
task = [ 3 , 4 , 7 , 15 ]
D = 10
print (minimumTask(task, D))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static bool
valid( int per_day, List< int > task, int d)
{
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 (cur_day > d) {
return false ;
}
}
return cur_day <= d;
}
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]);
}
int per_day_task = 0;
while (left <= right) {
int mid = left + (right - left) / 2;
if (valid(mid, task, d)) {
per_day_task = mid;
right = mid - 1;
}
else {
left = mid + 1;
}
}
return per_day_task;
}
public static void Main(String []args)
{
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));
}
}
|
Javascript
<script>
function valid(per_day,
task, d) {
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 (cur_day > d) {
return false ;
}
}
return cur_day <= d;
}
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]);
}
let per_day_task = 0;
while (left <= right) {
let mid = left
+ (right - left) / 2;
if (valid(mid, task, d)) {
per_day_task = mid;
right = mid - 1;
}
else {
left = mid + 1;
}
}
return per_day_task;
}
let task = [3, 4, 7, 15];
let D = 10;
document.write(minimumTask(task, D) + '<br>' );
</script>
|
Time Complexity: O(N * logN)
Auxiliary Space: O(1)
Last Updated :
14 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...