Imagine you're managing a group expense tracking app where friends frequently lend money to each other. After a vacation or shared activities, you have a complex web of debts between people. Your task is to minimize the number of actual money transfers needed to settle all debts.
You are given an array of transactions transactions where transactions[i] = [fromi, toi, amounti] indicates that person with ID fromi gave amounti dollars to person with ID toi.
Goal: Return the minimum number of transactions required to settle all debts completely.
Example: If Alice owes Bob $10, Bob owes Charlie $10, instead of 2 separate transactions, Alice can pay Charlie directly - just 1 transaction!
Input & Output
Visualization
Time & Space Complexity
For each subset, we iterate through all its subsets. Total is sum of C(n,k)*2^k = 3^n
DP array of size 2^n to store minimum groups for each bitmask
Constraints
- 1 โค transactions.length โค 8
- transactions[i].length == 3
- 0 โค fromi, toi < 12
- fromi โ toi
- 1 โค amounti โค 100