Minimize time to complete N tasks when task done by each is given
Given N tasks and N people who can work on them. Each task requires A[i] (0 <= i <= N-1) units of work to complete and each person can do at most B[i] units of work per day. Assign a task to each person so that the total time taken to complete all the tasks is minimized. Find the minimum number of days taken to complete all the tasks.
Note: Every person has to do exactly one task and two or more people can’t work on the same task.
Examples:
Input: N = 3, A = {6, 3, 6}, B = {2, 6, 8}
Output: 2
Explanation: Assign 1st task to 2nd person, 2nd task to 1st person and 3rd task to 3rd person.
Days taken by 1st, 2nd and 3rd person will be: 2, 1, 1Input: N = 2, A = {100, 100}, B = {1, 200}
Output: 100
Explanation: Since the work required by both tasks is same, we can assign them in any order.
Approach: To solve the problem follow the below idea:
Assign maximum amount of work to the person who can do maximum units of work per day. This results in the calculation of minimum days required to complete all the tasks.
Follow the given steps to solve the problem:
- Sort both the arrays in decreasing order
- Iterate over both the arrays simultaneously to calculate the number of days
- If A[i] is less than or equal to B[i], then only one day is required to complete this task
- Else if A[i] % B[i] is not equal to zero then (A[i] / B[i]) + 1 days will be required
- Otherwise, A[i] / B[i] days will be required.
- The maximum value from the calculated days is the required answer.
Below is the implementation for the above approach:
C++
// C++ program for the above approach #include <iostream> #include <bits/stdc++.h> #include <algorithm> using namespace std; int solve( int N , int A[], int B[]) { // sorting A and B in decreasing order sort(A, A+N, greater< int >()); sort(B, B+N, greater< int >()); int res = 0; for ( int i = 0; i < N; i++) { //If A[i]<= B[i] then, only one day //is required to complete the task if (A[i] / float (B[i]) <= 1) { res = max(res,1); } else { // If A[i]<=B[i] then, only one day is required // to complete the task if (A[i] % B[i] != 0) res = max((A[i] / B[i]) + 1, res); else res = max(res, A[i] / B[i]); } } return res; } // Driver code int main() { int A[] = {1, 3, 9, 5, 3}; int B[] = {6, 1, 5, 8, 5}; int N = sizeof (A) / sizeof (A[0]); cout << solve(N, A, B); return 0; } // This code is contributed by Atul_kumar_Shrivastava |
Java
// Java program for the above approach import java.io.*; import java.util.*; class GFG { static int solve( int N, Integer[] A, Integer[] B) { // sorting A and B in decreasing order Arrays.sort(A, Collections.reverseOrder()); Arrays.sort(B, Collections.reverseOrder()); int res = 0 ; for ( int i = 0 ; i < N; i++) { // If A[i]<=B[i] then, only one day is required // to complete the task if ((A[i] / B[i]) < 1 ) { res = Math.max(res, 1 ); } else { if ((A[i] % B[i]) != 0 ) { res = Math.max(( int )(A[i] / B[i]) + 1 , res); } else { res = Math.max(res, ( int )(A[i] / B[i])); } } } return res; } public static void main(String[] args) { Integer[] A = { 1 , 3 , 9 , 5 , 3 }; Integer[] B = { 6 , 1 , 5 , 8 , 5 }; int N = A.length; // Function call System.out.print(solve(N, A, B)); } } // This code is contributed by lokeshmvs21. |
Python3
# Python3 program for # the above approach def solve(N, A, B): # Sorting A and B in decreasing order A.sort(reverse = True ) B.sort(reverse = True ) res = 0 for i in range ( 0 , N): # If A[i]<= B[i] then, only one day # is required to complete the task if (A[i] / B[i] < = 1 ): res = max (res, 1 ) else : if (A[i] % B[i] ! = 0 ): res = max ((A[i] / / B[i]) + 1 , res) else : res = max (res, A[i] / / B[i]) return res # Driver Code if __name__ = = '__main__' : A = [ 1 , 3 , 9 , 5 , 3 ] B = [ 6 , 1 , 5 , 8 , 5 ] N = len (A) # Function call print ( str (solve(N, A, B))) |
C#
// C# program for the above approach using System; public class GFG { static int solve( int N, int [] A, int [] B) { // sorting A and B in decreasing order Array.Sort(A); Array.Sort(B); Array.Reverse(A); Array.Reverse(B); int res = 0; for ( int i = 0; i < N; i++) { // If A[i]<=B[i] then, only one day is required // to complete the task if ((A[i] / B[i]) < 1) { res = Math.Max(res, 1); } else { if ((A[i] % B[i]) != 0) { res = Math.Max(( int )(A[i] / B[i]) + 1, res); } else { res = Math.Max(res, ( int )(A[i] / B[i])); } } } return res; } static public void Main (){ int [] A = { 1, 3, 9, 5, 3 }; int [] B = { 6, 1, 5, 8, 5 }; int N = A.Length; // Function call Console.Write(solve(N, A, B)); } } // This code is contributed by hrithikgarg03188. |
Javascript
<script> // JavaScript code to implement the approach function solve(N, A, B) { // sorting A and B in decreasing order A.sort(); B.sort(); A.reverse(); B.reverse(); let res = 0; for (let i = 0; i < N; i++) { // If A[i]<=B[i] then, only one day is required // to complete the task if ((A[i] / B[i]) < 1) { res = Math.max(res, 1); } else { if ((A[i] % B[i]) != 0) { res = Math.max(Math.floor(A[i] / B[i]) + 1, res); } else { res = Math.max(res, Math.floor(A[i] / B[i])); } } } return res; } // Driver code let A = [ 1, 3, 9, 5, 3 ]; let B = [ 6, 1, 5, 8, 5 ]; let N = A.length; // Function call document.write(solve(N, A, B)); // This code is contributed by sanjoy_62. </script> |
2
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Please Login to comment...