Given a 2D array, arr[][] of size N, each row of which consists of the time required to complete 3 different types of tasks A, B, and C. The row elements arr[i][0], arr[i][1] and arr[i][2] are the time required to complete the ith task of type A, B and C respectively. The task is to find the minimum completion time to complete exactly K tasks from the array based on the following conditions:
- The same type of task will operate at the same time.
- Tasks of type B can be performed only when K tasks of type A have already been completed.
- Tasks of type C can be performed only when K tasks of type B have already been completed.
Examples:
Input: N = 3, K = 2, arr[][] = {{1, 2, 2}, {3, 4, 1}, {3, 1, 2}}
Output: 7
Explanation:
Minimum time required to complete K tasks of type A = min(max(arr[0][0], arr[2][0]), max(arr[0][0], arr[1][0]), max(arr[1][0], arr[2][0])) = min(max(1, 3), max(1, 3), max(3, 3)) 3
Minimum time required to complete K tasks of type B = max(arr[0][1], arr[2][1]) = 2
Therefore, select items 1 and 3 as the K tasks to be completed for minimum time.
Therefore, time to complete K tasks of type C = max(arr[0][2], arr[2][2]) = 2
Therefore, minimum time to complete K(= 2) of all types = 3 + 2 + 2 = 7Input: N = 3, K = 3, arr[][] = {{2, 4, 5}, {4, 5, 4}, {3, 4, 5}}
Output: 14
Approach: The problem can be solved using Recursion. The idea is to either select the ith row from the given array or not. The following are the recurrence relation.
MinTime(TA, TB, TC, K, i) = min(MinTime(max(arr[i][0], TA), max(arr[i][1], TB), max(arr[i][2], TC), K-1, i-1), MinTime(TA, TB, TC, K, i-1))
MinTime(TA, TB, TC, K, i) Stores the minimum time to complete K tasks.
i stores the position of the ith row of the given array elements.Base Case: if K == 0:
return TA + TB + TC
if i <= 0:
return Infinite
Follow the steps below to solve the problem:
- Initialize a variable, say X and Y while traversing each row recursively using the above recurrence relation.
- X stores the minimum completion time by selecting the ith row of the given array.
- Y stores the minimum completion time by not selecting the ith row of the given array.
- Find the value of X and Y using the above-mentioned recurrence relation.
- Finally, return the value of min(X, Y) for every row.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach#include<bits/stdc++.h>using namespace std;#define N 3// Function to get the minimum// completion time to select // exactly K itemsint MinTime(int arr[][N],int i, int Ta, int Tb, int Tc, int K){ // Base case if(K == 0) { return Ta + Tb + Tc; } if(i == 0) return INT_MAX; // Select the ith item int X = MinTime(arr, i - 1, max(Ta, arr[i - 1][0]), max(Tb, arr[i - 1][1]), max(Tc, arr[i - 1][2]), K -1); // Do not select the ith item int Y = MinTime(arr, i - 1, Ta, Tb, Tc, K); return min(X, Y);}// Driver Codeint main(){ int K = 2; int n = 3; int arr[N][N] = {{1, 2, 2} , {3, 4, 1} , {3, 1, 2} }; cout<<MinTime(arr, N, 0, 0, 0, K); return 0;} |
Java
// Java program to implement // the above approach import java.util.*;class GFG{// Function to get the minimum// completion time to select// exactly K itemspublic static int MinTime(int arr[][], int i, int Ta, int Tb, int Tc, int K){ // Base case if (K == 0) { return Ta + Tb + Tc; } if (i == 0) return Integer.MAX_VALUE; // Select the ith item int X = MinTime(arr, i - 1, Math.max(Ta, arr[i - 1][0]), Math.max(Tb, arr[i - 1][1]), Math.max(Tc, arr[i - 1][2]), K - 1); // Do not select the ith item int Y = MinTime(arr, i - 1, Ta, Tb, Tc, K); return Math.min(X, Y);}// Driver Codepublic static void main(String args[]){ int K = 2; int n = 3; int arr[][] = { { 1, 2, 2 }, { 3, 4, 1 }, { 3, 1, 2 } }; System.out.println(MinTime(arr, n, 0, 0, 0, K));}}// This code is contributed by hemanth gadarla |
Python3
# Python3 program to implement# the above approachN = 3# Function to get the minimum# completion time to select# exactly K itemsdef MinTime(arr, i, Ta, Tb, Tc, K): # Base cas if (K == 0): return Ta + Tb + Tc if (i == 0): return 10**9 # Select the ith item X = MinTime(arr, i - 1, max(Ta, arr[i - 1][0]), max(Tb, arr[i - 1][1]), max(Tc, arr[i - 1][2]), K -1) # Do not select the ith item Y = MinTime(arr, i - 1, Ta, Tb, Tc, K) return min(X, Y)# Driver Codeif __name__ == '__main__': K = 2 n = 3 arr = [ [ 1, 2, 2 ], [ 3, 4, 1 ], [ 3, 1, 2 ] ] print(MinTime(arr, N, 0, 0, 0, K))# This code is contributed by mohit kumar 29 |
C#
// C# program to implement // the above approach using System;class GFG{// Function to get the minimum// completion time to select// exactly K itemspublic static int MinTime(int [,]arr, int i, int Ta, int Tb, int Tc, int K){ // Base case if (K == 0) { return Ta + Tb + Tc; } if (i == 0) return int.MaxValue; // Select the ith item int X = MinTime(arr, i - 1, Math.Max(Ta, arr[i - 1, 0]), Math.Max(Tb, arr[i - 1, 1]), Math.Max(Tc, arr[i - 1, 2]), K - 1); // Do not select the ith item int Y = MinTime(arr, i - 1, Ta, Tb, Tc, K); return Math.Min(X, Y);}// Driver Codepublic static void Main(String []args){ int K = 2; int n = 3; int [,]arr = {{1, 2, 2}, {3, 4, 1}, {3, 1, 2}}; Console.WriteLine(MinTime(arr, n, 0, 0, 0, K));}}// This code is contributed by shikhasingrajput |
7
Time Complexity: O(2N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Total number of days taken to complete the task if after certain days one person leaves
- Minimum time required to complete a work by N persons together
- Minimum operations of the given type required to make a complete graph
- Print updated levels of each node of a Complete Binary Tree based on difference in weights of subtrees
- Print completed tasks at end according to Dependencies
- Count of minimum reductions required to get the required sum K
- Sort numbers based on count of letters required to represent them in words
- Minimum time required to rot all oranges
- Minimum time required to fill a cistern using N pipes
- Minimum time required to rot all oranges | Dynamic Programming
- Minimum time required to cover a Binary Array
- Minimum time required to visit all the special nodes of a Tree
- Program to find the time remaining for the day to complete
- Minimum time to reach a point with +t and -t moves at time t
- Minimum number of steps required to obtain the given Array by the given operations
- Steps required to visit M points in order on a circular ring of N points
- Minimize Steps required to obtain Sorted Order of an Array
- Sorting rows of matrix in ascending order followed by columns in descending order
- Sorting rows of matrix in descending order followed by columns in ascending order
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

