Optimal Account Balancing - Problem

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

example_1.py โ€” Simple Triangle
$ Input: transactions = [[0,1,10],[2,0,5]]
โ€บ Output: 2
๐Ÿ’ก Note: Person 0 gave $10 to person 1 and received $5 from person 2. Net: Person 0 owes $5, Person 1 is owed $10, Person 2 owes $5. Optimal: Person 0 pays Person 1 $5, Person 2 pays Person 1 $5. Total: 2 transactions.
example_2.py โ€” Linear Chain
$ Input: transactions = [[0,1,10],[1,0,1],[1,2,5],[2,0,5]]
โ€บ Output: 1
๐Ÿ’ก Note: After all transactions: Person 0 has net 0, Person 1 owes $4, Person 2 is owed $4. Only 1 transaction needed: Person 1 pays Person 2 $4.
example_3.py โ€” Already Balanced
$ Input: transactions = [[0,1,5],[1,0,5]]
โ€บ Output: 0
๐Ÿ’ก Note: All debts are already settled. Person 0 gave $5 to Person 1, then Person 1 gave $5 back to Person 0. No additional transactions needed.

Visualization

Tap to expand
Debt Settlement Network OptimizationAOwes $5BGets $10COwes $5A โ†’ B: $10C โ†’ A: $5Optimal Settlement StrategyA-5B+10C-5$5$5๐ŸŽฏ Result: 2 transactions instead of complex webA pays B $5, C pays B $5All debts settled with minimum transfers!
Understanding the Visualization
1
Calculate Net Balances
First, we compute each person's net balance (money owed minus money to receive)
2
Identify Settlement Groups
Find groups of people whose balances sum to zero - they can settle among themselves
3
Apply DP Optimization
Use dynamic programming to find the minimum number of such groups
4
Calculate Transactions
Each group of size k needs exactly k-1 transactions to settle internally
Key Takeaway
๐ŸŽฏ Key Insight: We only care about net balances, not individual transactions. By finding groups of people whose net balances sum to zero, we can minimize the total number of settlement transactions needed.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(3^n)

For each subset, we iterate through all its subsets. Total is sum of C(n,k)*2^k = 3^n

n
2n
โœ“ Linear Growth
Space Complexity
O(2^n)

DP array of size 2^n to store minimum groups for each bitmask

n
2n
โš  Quadratic Space

Constraints

  • 1 โ‰ค transactions.length โ‰ค 8
  • transactions[i].length == 3
  • 0 โ‰ค fromi, toi < 12
  • fromi โ‰  toi
  • 1 โ‰ค amounti โ‰ค 100
Asked in
Google 12 Amazon 8 Meta 6 Microsoft 4
23.5K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen