← 返回 salesforce 的题目列表Optimal Account Balancing
类型:online_judge
Given a list of transactions, where transactions[i] = [from_i, to_i, amount_i] means that person from_i paid amount_i to person to_i.
Return the minimum number of new transactions required to settle all debts. You may create settlement transactions between any two people; the only requirement is that every person's final net balance becomes zero.
Example 1
Input: transactions = [[0,1,10],[2,0,5]]
Output: 2
Explanation: The final balances are: person 0 should receive 5, person 1 should pay 10, and person 2 should receive 5.
One valid settlement is 1 -> 0 for 5 and 1 -> 2 for 5, requiring 2 transactions.
Example 2
Input: transactions = [[0,1,10],[1,0,1],[1,2,5],[2,0,5]]
Output: 1
Explanation: A single payment of 4 from person 1 to person 0 settles every balance.
Constraints
1 <= transactions.length <= 8
transactions[i].length == 3
0 <= from_i, to_i <= 20
from_i != to_i
1 <= amount_i <= 100
The input is valid and all balances can be settled.
Example
Input
[[0,1,10],[2,0,5]]
Output
2