Minimum time to complete at least K tasks when everyone rest after each task
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 tasks 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], k = 2
Output: 2
Approach - Using Binary Search
The idea is to use binary search as we observe the problem, we note that if a given time mid is sufficient, then all greater times will also be sufficient, making it suitable for binary search. We set the search space between min(arr) * k and max(arr) * k and use binary search to find the smallest valid time. For each mid value, we check if k tasks can be completed by counting tasks each worker can do within mid time. If mid is valid, we reduce the time range; otherwise, we increase it, ensuring an optimal solution efficiently.
// C++ program to find the minimum time required
// to complete k tasks using Binary Search
#include <bits/stdc++.h>
using namespace std;
// Function to check if it is possible to complete
// at least k tasks within the given time mid
bool timePossible(int mid, int k, vector<int>& arr,
vector<int>& restTime) {
int curTask = 0;
// Loop through each worker
for (int i = 0; i < arr.size(); i++) {
int totalTime = arr[i] + restTime[i];
curTask += mid / totalTime;
// Check if remaining time allows an extra task
if (mid % totalTime >= arr[i])
curTask++;
// If required tasks are completed, return true
if (curTask >= k) {
return true;
}
}
return false;
}
// Function to find the minimum time required
// to complete k tasks
int minimumTimeTaken(vector<int>& arr,
vector<int>& restTime, int k) {
// Lower bound of time
int st = arr[0] * k;
// Upper bound of time
int end = arr.back() * k;
while (st <= end) {
int mid = st + (end - st) / 2;
// If mid time is possible, try minimizing it
if (timePossible(mid, k, arr, restTime))
end = mid - 1;
else
st = mid + 1;
}
// Minimum time required
return st;
}
// Driver code
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 program to find the minimum time required
// to complete k tasks using Binary Search
import java.util.*;
class GfG {
// Function to check if it is possible to complete
// at least k tasks within the given time mid
static boolean timePossible(int mid, int k,
int[] arr, int[] restTime) {
int curTask = 0;
// Loop through each worker
for (int i = 0; i < arr.length; i++) {
int totalTime = arr[i] + restTime[i];
curTask += mid / totalTime;
// Check if remaining time allows an extra task
if (mid % totalTime >= arr[i])
curTask++;
// If required tasks are completed, return true
if (curTask >= k) {
return true;
}
}
return false;
}
// Function to find the minimum time required
// to complete k tasks
static int minimumTimeTaken(int[] arr,
int[] restTime, int k) {
// Lower bound of time
int st = arr[0] * k;
// Upper bound of time
int end = arr[arr.length - 1] * k;
while (st <= end) {
int mid = st + (end - st) / 2;
// If mid time is possible, try minimizing it
if (timePossible(mid, k, arr, restTime))
end = mid - 1;
else
st = mid + 1;
}
// Minimum time required
return st;
}
// Driver code
public static void main(String[] args) {
int[] arr = { 1, 2, 4 };
int[] restTime = { 1, 2, 2 };
int k = 5;
System.out.println(minimumTimeTaken(arr, restTime, k));
}
}
# Python program to find the minimum time required
# to complete k tasks using Binary Search
def timePossible(mid, k, arr, restTime):
curTask = 0
# Loop through each worker
for i in range(len(arr)):
totalTime = arr[i] + restTime[i]
curTask += mid // totalTime
# Check if remaining time allows an extra task
if mid % totalTime >= arr[i]:
curTask += 1
# If required tasks are completed, return True
if curTask >= k:
return True
return False
# Function to find the minimum time required
# to complete k tasks
def minimumTimeTaken(arr, restTime, k):
# Lower bound of time
st = arr[0] * k
# Upper bound of time
end = arr[-1] * k
while st <= end:
mid = st + (end - st) // 2
# If mid time is possible, try minimizing it
if timePossible(mid, k, arr, restTime):
end = mid - 1
else:
st = mid + 1
# Minimum time required
return st
# Driver code
if __name__ == "__main__":
arr = [1, 2, 4]
restTime = [1, 2, 2]
k = 5
print(minimumTimeTaken(arr, restTime, k))
// C# program to find the minimum time required
// to complete k tasks using Binary Search
using System;
class GfG {
// Function to check if it is possible to complete
// at least k tasks within the given time mid
static bool TimePossible(int mid, int k,
int[] arr, int[] restTime) {
int curTask = 0;
// Loop through each worker
for (int i = 0; i < arr.Length; i++) {
int totalTime = arr[i] + restTime[i];
curTask += mid / totalTime;
// Check if remaining time allows an extra task
if (mid % totalTime >= arr[i])
curTask++;
// If required tasks are completed, return true
if (curTask >= k) {
return true;
}
}
return false;
}
// Function to find the minimum time required
// to complete k tasks
static int MinimumTimeTaken(int[] arr,
int[] restTime, int k) {
// Lower bound of time
int st = arr[0] * k;
// Upper bound of time
int end = arr[arr.Length - 1] * k;
while (st <= end) {
int mid = st + (end - st) / 2;
// If mid time is possible, try minimizing it
if (TimePossible(mid, k, arr, restTime))
end = mid - 1;
else
st = mid + 1;
}
// Minimum time required
return st;
}
// Driver code
public static void Main() {
int[] arr = { 1, 2, 4 };
int[] restTime = { 1, 2, 2 };
int k = 5;
Console.WriteLine(MinimumTimeTaken(arr, restTime, k));
}
}
// JavaScript program to find the minimum time required
// to complete k tasks using Binary Search
function timePossible(mid, k, arr, restTime) {
let curTask = 0;
// Loop through each worker
for (let i = 0; i < arr.length; i++) {
let totalTime = arr[i] + restTime[i];
curTask += Math.floor(mid / totalTime);
// Check if remaining time allows an extra task
if (mid % totalTime >= arr[i]) {
curTask++;
}
// If required tasks are completed, return true
if (curTask >= k) {
return true;
}
}
return false;
}
// Function to find the minimum time required
// to complete k tasks
function minimumTimeTaken(arr, restTime, k) {
// Lower bound of time
let st = arr[0] * k;
// Upper bound of time
let end = arr[arr.length - 1] * k;
while (st <= end) {
let mid = st + Math.floor((end - st) / 2);
// If mid time is possible, try minimizing it
if (timePossible(mid, k, arr, restTime)) {
end = mid - 1;
} else {
st = mid + 1;
}
}
// Minimum time required
return st;
}
// Driver code
let arr = [1, 2, 4];
let restTime = [1, 2, 2];
let k = 5;
console.log(minimumTimeTaken(arr, restTime, k));
Output
5
Time Complexity: O(n log (k * max(arr))), where n is the number of workers, and k * max(arr) is the search space.
Auxiliary Space: O(1), as only a few extra variables are used.