📌  相关文章
📜  最小化完成所有流程所需的成本

📅  最后修改于: 2021-05-17 03:34:01             🧑  作者: Mango

给定一个二维数组arr [] [] ,每行的格式为{X,Y} ,其中YX分别代表启动过程所需的最低成本和完成该过程所花费的总成本。任务是找到可以按任意顺序完成过程的最低成本,以完成给定阵列的所有过程。

例子

方法:可以使用贪婪技术解决问题。请按照以下步骤解决问题:

  • 以Y的降序对数组进行排序。
  • 初始化一个变量,例如minCost ,以存储完成所有过程所需的最低成本。
  • 初始化一个变量,例如minCostInit ,以存储启动进程的最低成本。
  • 使用变量i遍历数组。对于i迭代,请检查minCostInit是否小于arr [i] [1] 。如果确定为true,则将minCost的值增加(arr [i] [1] – minCostInit)并更新minCostInit = arr [i] [1]
  • i迭代中,还更新minCostInit-= arr [i] [0]的值
  • 最后,打印minCost的值。

下面是上述方法的实现。

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
bool func(pair i1,
          pair i2)
{
    return (i1.first - i1.second <
            i2.first - i2.second);
}
 
// Function to find minimum cost required
// to complete all the process
int minimumCostReqToCompthePrcess(
    vector> arr)
{
     
    // Sort the array on descending order of Y
    sort(arr.begin(), arr.end(), func);
 
    // Stores length of array
    int n = arr.size();
 
    // Stores minimum cost required to
    // compleate all the process
    int minCost = 0;
 
    // Stores minimum cost to initiate
    // any process
    int minCostInit = 0;
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // If minCostInit is less than
        // the cost to initiate the process
        if (arr[i].second > minCostInit)
        {
             
            // Update minCost
            minCost += (arr[i].second -
                        minCostInit);
 
            // Update minCostInit
            minCostInit = arr[i].second;
        }
 
        // Update minCostInit
        minCostInit -= arr[i].first;
    }
 
    // Return minCost
    return minCost;
}
 
// Driver Code
int main()
{
    vector> arr = { { 1, 2 },
                                   { 2, 4 },
                                   { 4, 8 } };
 
    // Function Call
    cout << (minimumCostReqToCompthePrcess(arr));
}
 
// This code is contributed by grand_master


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
 
  // Function to find minimum cost required
  // to complete all the process
  static int minimumCostReqToCompthePrcess(
    int[][] arr)
  {
 
    // Sort the array on descending order of Y
    Arrays.sort(arr, (a, b)->b[1]-a[1]);
 
    // Stores length of array
    int n = arr.length;
 
    // Stores minimum cost required to
    // compleate all the process
    int minCost = 0;
 
    // Stores minimum cost to initiate
    // any process
    int minCostInit = 0;
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
 
      // If minCostInit is less than
      // the cost to initiate the process
      if (arr[i][1] > minCostInit)
      {
 
        // Update minCost
        minCost += (arr[i][1] -
                    minCostInit);
 
        // Update minCostInit
        minCostInit = arr[i][1];
      }
 
      // Update minCostInit
      minCostInit -= arr[i][0];
    }
 
    // Return minCost
    return minCost;
  }
 
  // Driver code
  public static void main (String[] args)
  {
    int[][] arr = { { 1, 2 },
                   { 2, 4 },
                   { 4, 8 } };
 
    // Function Call
    System.out.println(minimumCostReqToCompthePrcess(arr));
  }
}
 
// This code is contributed by offbeat


Python3
# Python3 program to implement
# the above approach
 
 
# Function to find minimum cost required
# to complete all the process
def minimumCostReqToCompthePrcess(arr):
 
 
    # Sort the array on descending order of Y
    arr.sort(key = lambda x: x[0] - x[1])
     
     
    # Stores length of array
    n = len(arr)
     
     
    # Stores minimum cost required to
    # compleate all the process
    minCost = 0
     
     
    # Stores minimum cost to initiate
    # any process
    minCostInit = 0
     
 
    # Traverse the array
    for i in range(n):
 
 
        # If minCostInit is less than
        # the cost to initiate the process
        if arr[i][1] > minCostInit:
 
 
            # Update minCost
            minCost += (arr[i][1]
                       - minCostInit)
             
             
            # Update minCostInit
            minCostInit = arr[i][1]
 
 
        # Update minCostInit
        minCostInit -= arr[i][0]
 
 
    # Return minCost
    return minCost
 
 
# Driver Code
if __name__ == "__main__":
    arr = [[1, 2], [2, 4], [4, 8]]
 
 
    # Function Call
    print(minimumCostReqToCompthePrcess(arr))


输出:
8

时间复杂度: O(N * log(N))
辅助空间: O(1)