Minimum time to complete at least K tasks when everyone rest after each task
Last Updated :
09 May, 2022
Given an array arr[] of size N representing the time taken by a person to complete a task. Also, an array restTime[] which denotes the amount of time one person takes to rest after finishing a task. Each person is independent of others i.e. they can work simultaneously on different task at the same time. Given an integer K, the task is to find at least how much time will be taken to complete all the K tasks by all the persons.
Examples:
Input: arr[] = {1, 2, 4}, restTime[] = {1, 2, 2}, K = 5
Output: 5
Explanation: At t = 1, tasks task done = [1, 0, 0], Total task = 1
At t = 2, No of tasks completed = [1, 1, 0],
Total task = 1 + 1 = 2. Because 1st person was taking rest
At t = 3, No of tasks completed = [2, 1, 0],
Total task = 2 + 1 = 3, Because 2nd person was taking rest
At t = 4, No of tasks completed = [2, 1, 1],
Total task = 2 + 1 + 1 = 4, Because 1st and 2nd person was taking rest
At t = 5, No of tasks completed = [3, 1, 1].
Total task = 3 + 1 + 1 = 5. Minimum time taken = 5.
Input: arr[] = {1, 2, 4, 7, 8}, restTime[] = {4, 1, 2, 3, 1}, TotalTask = 2
Output: 2
Approach: The problem can be solved using Binary Search, based on the following observations:
The minimum time taken can be 1 and maximum time required can be very large. If in time x all the K tasks can be completed, then it is also possible that they can be completed in less than x time. Use this concept to apply binary search on the time required.
Follow the below steps to implement the above approach:
- Create a variable st = 1, represent the starting point of the time range and end = INT_MAX, the ending point.
- Use binary search in the range from st to end:
- Calculate mid = st + (end – st)/2
- Check if K tasks can be completed in mid amount of time:
- If it can be completed then set end = mid-1.
- Else set st = mid+1.
- Return st at the end as this will be the minimum time required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool timePossible( int mid, int totalTask,
vector< int >& timeArr,
vector< int >& restTime)
{
int curTask = 0;
for ( int i = 0; i < timeArr.size(); i++) {
int totalTime
= timeArr[i] + restTime[i];
curTask += mid / totalTime;
if (mid % totalTime >= timeArr[i])
curTask++;
if (curTask >= totalTask) {
return true ;
}
}
if (curTask >= totalTask) {
return true ;
}
return false ;
}
int minimumTimeTaken(vector< int >& timeArr,
vector< int >& restTime,
int totalTask)
{
int st = 1;
int end = INT_MAX;
int minimumtime = 0;
while (st <= end) {
int mid = st + (end - st) / 2;
if (timePossible(mid, totalTask,
timeArr, restTime))
end = mid - 1;
else
st = mid + 1;
}
return st;
}
int main()
{
vector< int > arr = { 1, 2, 4 };
vector< int > restTime = { 1, 2, 2 };
int K = 5;
cout << minimumTimeTaken(arr,
restTime, K);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static boolean timePossible( int mid,
int totalTask,
int timeArr[],
int restTime[])
{
int curTask = 0 ;
for ( int i = 0 ; i < timeArr.length; i++) {
int totalTime = timeArr[i] + restTime[i];
curTask += mid / totalTime;
if (mid % totalTime >= timeArr[i])
curTask++;
if (curTask >= totalTask) {
return true ;
}
}
if (curTask >= totalTask) {
return true ;
}
return false ;
}
public static int minimumTimeTaken( int timeArr[],
int restTime[],
int totalTask)
{
int st = 1 ;
int end = Integer.MAX_VALUE;
int minimumtime = 0 ;
while (st <= end) {
int mid = st + (end - st) / 2 ;
if (timePossible(mid, totalTask, timeArr,
restTime)
!= false )
end = mid - 1 ;
else
st = mid + 1 ;
}
return st;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 4 };
int restTime[] = { 1 , 2 , 2 };
int K = 5 ;
System.out.print(
minimumTimeTaken(arr, restTime, K));
}
}
|
Python3
INT_MAX = 2147483647
def timePossible(mid, totalTask, timeArr, restTime):
curTask = 0
for i in range ( 0 , len (timeArr)):
totalTime = timeArr[i] + restTime[i]
curTask + = mid / / totalTime
if (mid % totalTime > = timeArr[i]):
curTask + = 1
if (curTask > = totalTask):
return True
if (curTask > = totalTask):
return True
return False
def minimumTimeTaken(timeArr, restTime, totalTask):
st = 1
end = INT_MAX
minimumtime = 0
while (st < = end):
mid = st + (end - st) / / 2
if (timePossible(mid, totalTask,
timeArr, restTime)):
end = mid - 1
else :
st = mid + 1
return st
if __name__ = = "__main__" :
arr = [ 1 , 2 , 4 ]
restTime = [ 1 , 2 , 2 ]
K = 5
print (minimumTimeTaken(arr, restTime, K))
|
C#
using System;
class GFG
{
public static bool timePossible( int mid,
int totalTask,
int [] timeArr,
int [] restTime)
{
int curTask = 0;
for ( int i = 0; i < timeArr.Length; i++) {
int totalTime = timeArr[i] + restTime[i];
curTask += mid / totalTime;
if (mid % totalTime >= timeArr[i])
curTask++;
if (curTask >= totalTask) {
return true ;
}
}
if (curTask >= totalTask) {
return true ;
}
return false ;
}
public static int minimumTimeTaken( int [] timeArr,
int [] restTime,
int totalTask)
{
int st = 1;
int end = Int32.MaxValue;
int minimumtime = 0;
while (st <= end) {
int mid = st + (end - st) / 2;
if (timePossible(mid, totalTask, timeArr,
restTime)
!= false )
end = mid - 1;
else
st = mid + 1;
}
return st;
}
public static void Main()
{
int [] arr = { 1, 2, 4 };
int [] restTime = { 1, 2, 2 };
int K = 5;
Console.Write(
minimumTimeTaken(arr, restTime, K));
}
}
|
Javascript
<script>
function timePossible(mid, totalTask, timeArr, restTime)
{
let curTask = 0;
for (let i = 0; i < timeArr.length; i++)
{
let totalTime = timeArr[i] + restTime[i];
curTask += Math.floor(mid / totalTime);
if (mid % totalTime >= timeArr[i])
curTask++;
if (curTask >= totalTask) {
return true ;
}
}
if (curTask >= totalTask) {
return true ;
}
return false ;
}
function minimumTimeTaken(timeArr, restTime, totalTask)
{
let st = 1;
let end = Number.MAX_VALUE;
let minimumtime = 0;
while (st <= end) {
let mid = st + Math.floor((end - st) / 2);
if (timePossible(mid, totalTask,
timeArr, restTime))
end = mid - 1;
else
st = mid + 1;
}
return st;
}
let arr = [ 1, 2, 4 ];
let restTime = [ 1, 2, 2 ];
let K = 5;
document.write(minimumTimeTaken(arr, restTime, K));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...