Minimize cost required to complete all processes
Last Updated :
19 Dec, 2022
Given a 2D array arr[][] with each row of the form { X, Y }, where Y and X represents the minimum cost required to initiate a process and the total cost spent to complete the process respectively. The task is to find the minimum cost required to complete all the process of the given array if processes can be completed in any order.
Examples:
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
Approach: The problem can be solved using Greedy technique. Follow the steps below to solve the problem:
- Sort the array in descending order of Y.
- Initialize a variable, say minCost, to store the minimum cost required to complete all the process.
- Initialize a variable, say minCostInit, to store the minimum cost to initiate a process.
- Traverse the array using variable i. For every ith iteration, check if minCostInit is less than arr[i][1] or not. If found to be true then increment the value of minCost by (arr[i][1] – minCostInit) and update minCostInit = arr[i][1].
- In every ith iteration also update the value of minCostInit -= arr[i][0].
- Finally, print the value of minCost.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
bool func(pair< int , int > i1,
pair< int , int > i2)
{
return (i1.first - i1.second <
i2.first - i2.second);
}
int minimumCostReqToCompthePrcess(
vector<pair< int , int >> arr)
{
sort(arr.begin(), arr.end(), func);
int n = arr.size();
int minCost = 0;
int minCostInit = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i].second > minCostInit)
{
minCost += (arr[i].second -
minCostInit);
minCostInit = arr[i].second;
}
minCostInit -= arr[i].first;
}
return minCost;
}
int main()
{
vector<pair< int , int >> arr = { { 1, 2 },
{ 2, 4 },
{ 4, 8 } };
cout << (minimumCostReqToCompthePrcess(arr));
}
|
Java
import java.util.*;
class GFG
{
static int minimumCostReqToCompthePrcess(
int [][] arr)
{
Arrays.sort(arr, (a, b)->b[ 1 ]-a[ 1 ]);
int n = arr.length;
int minCost = 0 ;
int minCostInit = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i][ 1 ] > minCostInit)
{
minCost += (arr[i][ 1 ] -
minCostInit);
minCostInit = arr[i][ 1 ];
}
minCostInit -= arr[i][ 0 ];
}
return minCost;
}
public static void main (String[] args)
{
int [][] arr = { { 1 , 2 },
{ 2 , 4 },
{ 4 , 8 } };
System.out.println(minimumCostReqToCompthePrcess(arr));
}
}
|
Python3
def minimumCostReqToCompthePrcess(arr):
arr.sort(key = lambda x: x[ 0 ] - x[ 1 ])
n = len (arr)
minCost = 0
minCostInit = 0
for i in range (n):
if arr[i][ 1 ] > minCostInit:
minCost + = (arr[i][ 1 ]
- minCostInit)
minCostInit = arr[i][ 1 ]
minCostInit - = arr[i][ 0 ]
return minCost
if __name__ = = "__main__" :
arr = [[ 1 , 2 ], [ 2 , 4 ], [ 4 , 8 ]]
print (minimumCostReqToCompthePrcess(arr))
|
C#
using System;
class GFG
{
static int compare( int [] a, int [] b)
{
return b[1] - a[1];
}
static int minimumCostReqToCompthePrcess(
int [][] arr)
{
Array.Sort(arr, compare);
int n = arr.Length;
int minCost = 0;
int minCostInit = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i][1] > minCostInit)
{
minCost += (arr[i][1] -
minCostInit);
minCostInit = arr[i][1];
}
minCostInit -= arr[i][0];
}
return minCost;
}
public static void Main ( string [] args)
{
int [][] arr = { new int [] { 1, 2 },
new int [] { 2, 4 },
new int [] { 4, 8 } };
Console.WriteLine(minimumCostReqToCompthePrcess(arr));
}
}
|
Javascript
<script>
function minimumCostReqToCompthePrcess(arr)
{
arr=arr.map(row=>row).reverse()
var n = arr.length;
var minCost = 0;
var minCostInit = 0;
for ( var i = 0; i < n; i++)
{
if (arr[i][1] > minCostInit)
{
minCost += (arr[i][1] -
minCostInit);
minCostInit = arr[i][1];
}
minCostInit -= arr[i][0];
}
return minCost;
}
var arr = [ [ 1, 2 ],
[ 2, 4 ],
[ 4, 8 ] ];
document.write(minimumCostReqToCompthePrcess(arr));
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...