给定一个二维数组arr[][] ,每行的形式为{ X, Y } ,其中Y和X 分别表示启动流程所需的最低成本和完成流程所花费的总成本。任务是找到完成给定数组的所有过程所需的最小成本,如果过程可以按任何顺序完成。
例子:
Input: arr[][] = { { 1, 2 }, { 2, 4 }, { 4, 8 } }
Output: 8
Explanation:
Consider an initial cost of 8.
Initiate the process arr[2] and after finishing the process, remaining cost = 8 – 4 = 4
Initiate the process arr[1] and after finishing the process, remaining cost = 4 – 2 = 2
Initiate the process arr[0] and after finishing the process, remaining cost = 2 – 1 = 1
Therefore, the required output is 8.
Input: arr[][] = { { 1, 7 }, { 2, 8 }, { 3, 9 }, { 4, 10 }, { 5, 11 }, { 6, 12 } }
Output: 27
方法:该问题可以使用贪心技术解决。请按照以下步骤解决问题:
- 按 Y 的降序对数组进行排序。
- 初始化一个变量,比如minCost ,以存储完成所有过程所需的最低成本。
- 初始化一个变量,比如minCostInit ,以存储启动进程的最小成本。
- 使用变量i遍历数组。对于每第i次迭代,检查minCostInit是否小于arr[i][1] 。如果发现为真,则将 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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live