Minimize cost required to complete all processes | 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
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 ) ) ;