A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for Bill’s lunch for $10. Then later Chris gave Alice $5 for a taxi ride. We can model each transaction as a tuple (x, y, z) which means person x gave person y $z. Assuming Alice, Bill, and Chris are person 0, 1, and 2 respectively (0, 1, 2 are the person’s ID), the transactions can be represented as [[0, 1, 10], [2, 0, 5]].
Given a list of transactions between a group of people, return the minimum number of transactions required to settle the debt.
Note:
A transaction will be given as a tuple (x, y, z). Note that x ≠ y and z > 0.
Person’s IDs may not be linear, e.g. we could have the persons 0, 1, 2 or we could also have the persons 0, 2, 6.
Input: [[0,1,10],[2,0,5]]Output: 2Explanation:
Person #0 gave person #1 $10.Person #2 gave person #0 $5.Two transactions are needed. One way to settle the debt is person #1 pays person #0 and #2 $5 each.
Example 2:
1
2
3
4
5
6
7
8
9
Input: [[0,1,10],[1,0,1],[1,2,5],[2,0,5]]Output: 1Explanation:
Person #0 gave person #1 $10.Person #1 gave person #0 $1.Person #1 gave person #2 $5.Person #2 gave person #0 $5.Therefore, person #1 only need to give person #0 $4, and all debt is settled.
To minimize the number of transactions to settle all debts, we first compute each person’s net balance. We then use dynamic programming with bitmasking to find the optimal way to group zero-sum subsets, minimizing the number of transactions needed.
classSolution {
public:int minTransfers(vector<vector<int>>& transactions) {
int g[12]{};
for (auto& t : transactions) {
g[t[0]] -= t[2];
g[t[1]] += t[2];
}
vector<int> nums;
for (int x : g) {
if (x) {
nums.push_back(x);
}
}
int m = nums.size();
int f[1<< m];
memset(f, 0x3f, sizeof(f));
f[0] =0;
for (int i =1; i <1<< m; ++i) {
int s =0;
for (int j =0; j < m; ++j) {
if (i >> j &1) {
s += nums[j];
}
}
if (s ==0) {
f[i] = __builtin_popcount(i) -1;
for (int j = (i -1) & i; j; j = (j -1) & i) {
f[i] = min(f[i], f[j] + f[i ^ j]);
}
}
}
return f[(1<< m) -1];
}
};
classSolution {
funminTransfers(transactions: List<List<Int>>): Int {
val g = IntArray(12)
for ((f, t, x) in transactions) {
g[f] -= x
g[t] += x
}
val nums = g.filter { it!=0 }
val m = nums.size
val f = IntArray(1 shl m) { Int.MAX_VALUE / 2 }
f[0] = 0for (i in1 until (1 shl m)) {
var s = 0for (j in0 until m) {
if ((i shr j) and 1==1) s += nums[j]
}
if (s ==0) {
f[i] = Integer.bitCount(i) - 1var j = (i - 1) and i
while (j > 0) {
f[i] = minOf(f[i], f[j] + f[i xor j])
j = (j - 1) and i
}
}
}
return f[(1 shl m) - 1]
}
}
from collections import defaultdict
from math import inf
classSolution:
defminTransfers(self, transactions: List[List[int]]) -> int:
g = defaultdict(int)
for f, t, x in transactions:
g[f] -= x
g[t] += x
nums = [x for x in g.values() if x]
m = len(nums)
f = [inf] * (1<< m)
f[0] =0for i in range(1, 1<< m):
s =0for j, x in enumerate(nums):
if i >> j &1:
s += x
if s ==0:
f[i] = i.bit_count() -1 j = (i -1) & i
while j >0:
f[i] = min(f[i], f[j] + f[i ^ j])
j = (j -1) & i
return f[-1]
⏰ Time complexity: O(3^N), where N is the number of people with non-zero balance. This comes from trying all possible subsets and partitions of debts, as each person can be included, excluded, or paired in each subset.
🧺 Space complexity: O(2^N), for the DP array that stores the minimum transactions for each subset of debts.