📌  相关文章
📜  根据给定的任务执行顺序完成 K 个任务所需的最短时间

📅  最后修改于: 2021-09-04 08:08:02             🧑  作者: Mango

给定一个大小为N二维数组arr[][] ,其中每一行包含完成3种不同类型的任务AB C。行元素 arr[i][0]、arr[i][1] 和 arr[i][2] 分别是完成类型A、BC 的i任务所需的时间。任务是根据以下条件从数组中找到完成K 个任务的最短完成时间:

  • 同一类型的任务将同时运行。
  • 只有已经完成了K个类型A 的任务,才能执行类型B 的任务。
  • 只有在完成了K 个B类任务后,才能执行C类任务。

例子:

方法:可以使用递归解决问题。我们的想法是无论是从给定的阵列或不选择第i行。以下是递推关系。

请按照以下步骤解决问题:

  1. 初始化一个变量,比如XY,同时使用上述递归关系递归遍历每一行。
  2. X通过选择给定数组的i行来存储最短完成时间。
  3. Y通过不选择给定数组的i行来存储最短完成时间。
  4. 使用上述递推关系求XY的值。
  5. 最后,为每一行返回min(X, Y)的值。

下面是上述方法的实现:

C++
// 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][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 Code
int main()
{
  int K = 2;
  int n = 3;
  int arr[N][N] = {{1, 2, 2} ,
                   {3, 4, 1} ,
                   {3, 1, 2}
                  };
   
  cout<


Java
// 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
# 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#
// 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


Javascript


C++
#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[0], {t[1], t[2]}});
    }
   
    // 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 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<


Java
// Java program to implement 
// the above approach
import java.util.*;
class GFG
{
  static int MinTime(int[][] tasks, int k)
  {
 
    // if k == 0 then return 0
    if (k == 0)
    {
      return 0;
    }
    ArrayList arr = new ArrayList<>();
 
    // make a pair of pair DS
    for (int[] t : tasks)
    {
      arr.add(new int[]{t[0], t[1], t[2]});
    }
 
    // sort the pairs
    Collections.sort(arr, (a, b)->a[0] - b[0]);
 
    // initialize the ans as INT_MAX/2
    int ans =Integer.MAX_VALUE / 2;
    for (int i = k - 1; i < arr.size(); i++)
    {
      ArrayList pr = new ArrayList<>();
 
      // push the pairs into the pr vector
      for (int j = 0; j <= i; j++)
      {
        pr.add(new int[]{arr.get(j)[1],arr.get(j)[2]});
      }
 
      // sort the pairs
      Collections.sort(pr, (a, b)->a[0] - b[0]);
 
      // priority queue
      PriorityQueue q = new PriorityQueue<>();
      for (int j = 0; j < pr.size(); j++)
      {
        q.add(pr.get(j)[1]);
        if (q.size() > k)
        {
          q.poll();
        }
        if (q.size() == k)
        {
          ans = Math.min(ans, arr.get(i)[0] +
                         q.peek() + pr.get(j)[0]);
        }
      }
    }
    return ans;
  }
 
  // 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,K));
 
  }
}
 
// This code is contributed by offbeat


输出
7

时间复杂度: O(2 N )
辅助空间: O(N)

方法2 :-

C++

#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[0], {t[1], t[2]}});
    }
   
    // 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 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<

Java

// Java program to implement 
// the above approach
import java.util.*;
class GFG
{
  static int MinTime(int[][] tasks, int k)
  {
 
    // if k == 0 then return 0
    if (k == 0)
    {
      return 0;
    }
    ArrayList arr = new ArrayList<>();
 
    // make a pair of pair DS
    for (int[] t : tasks)
    {
      arr.add(new int[]{t[0], t[1], t[2]});
    }
 
    // sort the pairs
    Collections.sort(arr, (a, b)->a[0] - b[0]);
 
    // initialize the ans as INT_MAX/2
    int ans =Integer.MAX_VALUE / 2;
    for (int i = k - 1; i < arr.size(); i++)
    {
      ArrayList pr = new ArrayList<>();
 
      // push the pairs into the pr vector
      for (int j = 0; j <= i; j++)
      {
        pr.add(new int[]{arr.get(j)[1],arr.get(j)[2]});
      }
 
      // sort the pairs
      Collections.sort(pr, (a, b)->a[0] - b[0]);
 
      // priority queue
      PriorityQueue q = new PriorityQueue<>();
      for (int j = 0; j < pr.size(); j++)
      {
        q.add(pr.get(j)[1]);
        if (q.size() > k)
        {
          q.poll();
        }
        if (q.size() == k)
        {
          ans = Math.min(ans, arr.get(i)[0] +
                         q.peek() + pr.get(j)[0]);
        }
      }
    }
    return ans;
  }
 
  // 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,K));
 
  }
}
 
// This code is contributed by offbeat
输出
7

时间复杂度:- O(N 2 logN)
空间复杂度:- O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live