# Minimum time required to complete exactly K tasks based on given order of task execution

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], arr[i] and arr[i] 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:
Explanation:
Minimum time required to complete K tasks of type A = min(max(arr, arr), max(arr, arr), max(arr, arr)) = min(max(1, 3), max(1, 3), max(3, 3)) 3
Minimum time required to complete K tasks of type B = max(arr, arr) = 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, arr) = 2
Therefore, minimum time to complete K(= 2) of all types = 3 + 2 + 2 = 7

Input: 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], TA), max(arr[i], TB), max(arr[i], 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:

1. Initialize a variable, say X and Y while traversing each row recursively using the above recurrence relation.
2. X stores the minimum completion time by selecting the ith row of the given array.
3. Y stores the minimum completion time by not selecting the ith row of the given array.
4. Find the value of X and Y using the above-mentioned recurrence relation.
5. Finally, return the value of min(X, Y) for every row.

Below is the implementation of the above approach:

 `// C++ program to implement ` `// the above approach` `#include` `using` `namespace` `std;` `#define N 3`   `// Function to get the minimum` `// completion time to select ` `// exactly K items` `int` `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]),` `            ``max(Tb, arr[i - 1]),` `            ``max(Tc, arr[i - 1]), K -1);` `    `  `    ``// Do not select the ith item` `    ``int` `Y = MinTime(arr, i - 1,` `                    ``Ta, Tb, Tc, K);` `                    `  `    ``return` `min(X, Y);` `}`   `// Driver Code` `int` `main()` `{` `  ``int` `K = 2;` `  ``int` `n = 3;` `  ``int` `arr[N][N] = {{1, 2, 2} ,` `                   ``{3, 4, 1} ,` `                   ``{3, 1, 2}` `                  ``};` `  `  `  ``cout<

 `// Java program to implement  ` `// the above approach ` `import` `java.util.*;`   `class` `GFG{`   `// Function to get the minimum` `// completion time to select` `// exactly K items` `public` `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 Code` `public` `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 program to implement` `# the above approach` `N ``=` `3`   `# Function to get the minimum` `# completion time to select` `# exactly K items` `def` `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 Code` `if` `__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# program to implement  ` `// the above approach ` `using` `System;` `class` `GFG{`   `// Function to get the minimum` `// completion time to select` `// exactly K items` `public` `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 Code` `public` `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`

Output
`7`

Time Complexity: O(2N)
Auxiliary Space: O(N)

Approach 2 :-

 `#include ` `#include `   `using` `namespace` `std;` `int` `MinTime(vector>& tasks, ``int` `k) ` `{` `    ``// if k == 0 then return 0` `    ``if` `(k == 0) ` `    ``{` `        ``return` `0;` `    ``}` `    ``vector>> arr;` `  `  `    ``// make a pair of pair DS` `    ``for` `(``auto` `t : tasks)` `    ``{` `        ``arr.push_back({t, {t, t}});` `    ``}` `  `  `    ``// sort the pairs` `    ``sort(arr.begin(), arr.end());` `  `  `    ``// initialize the ans as INT_MAX/2` `    ``int` `ans = INT_MAX / 2;` `    ``for` `(``int` `i = k - 1; i < arr.size(); i++) ` `    ``{` `        ``vector> pr;` `      `  `        ``// push the pairs into the pr vector` `        ``for` `(``int` `j = 0; j <= i; j++) ` `        ``{` `            ``pr.push_back(arr[j].second);` `        ``}` `      `  `        ``// sort the pairs` `        ``sort(pr.begin(), pr.end());` `       `  `        ``// priority queue` `        ``priority_queue<``int``> q;` `        ``for` `(``int` `j = 0; j < pr.size(); j++) ` `        ``{` `            ``q.push(pr[j].second);` `            ``if` `(q.size() > k) ` `            ``{` `                ``q.pop();` `            ``}` `            ``if` `(q.size() == k) ` `            ``{` `                ``ans = min(ans, arr[i].first + q.top() + pr[j].first);` `            ``}` `        ``}` `    ``}` `    ``return` `ans;` `}`   `// Driver Code` `int` `main() {`   `   ``int` `K = 2;` `  ``int` `n = 3;` `  ``vector> arr = ` `                  ``{{1, 2, 2} ,` `                   ``{3, 4, 1} ,` `                   ``{3, 1, 2}` `                  ``};` `   `  `  ``cout<

Output
```7
```

Time Complexity :- O(N2logN)
Space Complexity:- 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.

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.

Article Tags :