Minimize cost required to complete all processes | Java program for the above approach ; Function to find minimum cost required to complete all the process ; Sort the array on descending order of Y ; Stores length of array ; Stores minimum cost required to complete all the process ; Stores minimum cost to initiate any process ; Traverse the array ; If minCostInit is less than the cost to initiate the process ; Update minCost ; Update minCostInit ; Update minCostInit ; Return minCost ; Driver code ; Function Call
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 ) ) ; } }