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
def minimumCostReqToCompthePrcess ( arr ) : NEW_LINE INDENT arr . sort ( key = lambda x : x [ 0 ] - x [ 1 ] ) NEW_LINE n = len ( arr ) NEW_LINE minCost = 0 NEW_LINE minCostInit = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] [ 1 ] > minCostInit : NEW_LINE INDENT minCost += ( arr [ i ] [ 1 ] - minCostInit ) NEW_LINE minCostInit = arr [ i ] [ 1 ] NEW_LINE DEDENT minCostInit -= arr [ i ] [ 0 ] NEW_LINE DEDENT return minCost NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 1 , 2 ] , [ 2 , 4 ] , [ 4 , 8 ] ] NEW_LINE print ( minimumCostReqToCompthePrcess ( arr ) ) NEW_LINE DEDENT