题目描述
Given a number of friends who have to give or take some amount of money from one another. Design an algorithm by which the total cash flow among all the friends is minimized.
Example 1:
Input:
N=3
transaction [][]={{0,100,0}, {0,0,100}, {100,0,0}}
Output:
transaction [][]={{0,0,0},{0,0,0},{0,0,0}}
Explanation:
Since friend one has to give friend two which has to give friend three and which in turn has to give one. So it is better than no one will do anything to anyone.
Example 2:
Input:
N=3
transaction [][]={{0,100,0{,{0,0,200},{0,0,0}}
Output:
transaction [][]={0,0,100},{0,0,100},{0,0,0}
Explanation:
The net flow is minimized.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minCashFlow() which takes the transaction array and number of friends as input parameters and returns the new transaction array as output;.
Expected Time Complexity: O(NN)
Expected Auxiliary Space: O(NN)
Constraints:
1 <= N <= 1000
0^{ }<= transactio[i][j] <= 1000
输入格式
见题面
输出格式
见题面