|
| 1 | +from typing import List, DefaultDict |
| 2 | +from collections import defaultdict |
| 3 | + |
| 4 | + |
| 5 | +def min_transfers_dfs(transactions: List[List[int]]) -> int: |
| 6 | + if not transactions: |
| 7 | + return 0 |
| 8 | + |
| 9 | + # Net balances where the key is the person and the value is their balance |
| 10 | + net_balances: DefaultDict[int, int] = defaultdict(int) |
| 11 | + |
| 12 | + # Populate net balances |
| 13 | + for transaction in transactions: |
| 14 | + sender, receiver, amount = transaction |
| 15 | + net_balances[sender] -= amount |
| 16 | + net_balances[receiver] += amount |
| 17 | + |
| 18 | + # Remove zero balances |
| 19 | + non_zero_balances = [amt for amt in net_balances.values() if amt != 0] |
| 20 | + number_of_non_zero_balances = len(non_zero_balances) |
| 21 | + |
| 22 | + if non_zero_balances == 0: |
| 23 | + return 0 |
| 24 | + |
| 25 | + def dfs(current: int, balances: List[int]) -> int: |
| 26 | + # Skip settled accounts, move to the next person with non-zero balaance |
| 27 | + while current < number_of_non_zero_balances and balances[current] == 0: |
| 28 | + current += 1 |
| 29 | + # All accounts settled |
| 30 | + if current >= number_of_non_zero_balances: |
| 31 | + return 0 |
| 32 | + |
| 33 | + min_transactions = float("inf") |
| 34 | + # Try to settle non_zero_balances[start] by paring with another opposite sign balance |
| 35 | + for j in range(current + 1, number_of_non_zero_balances): |
| 36 | + # One owes, the other is owed |
| 37 | + if balances[current] * balances[j] < 0: |
| 38 | + balances[j] += balances[current] |
| 39 | + # recurse for remaining transactions after settling this one |
| 40 | + min_transactions = min(min_transactions, 1 + dfs(current + 1, balances)) |
| 41 | + # backtrack, undo the settlement |
| 42 | + non_zero_balances[j] -= non_zero_balances[current] |
| 43 | + |
| 44 | + return min_transactions |
| 45 | + |
| 46 | + return dfs(0, non_zero_balances) |
| 47 | + |
| 48 | + |
| 49 | +def min_transfers_backtrack(transactions: List[List[int]]) -> int: |
| 50 | + if not transactions: |
| 51 | + return 0 |
| 52 | + |
| 53 | + # Net balances where the key is the person and the value is their balance |
| 54 | + net_balances: DefaultDict[int, int] = defaultdict(int) |
| 55 | + |
| 56 | + # Populate net balances |
| 57 | + for transaction in transactions: |
| 58 | + sender, receiver, amount = transaction |
| 59 | + net_balances[sender] -= amount |
| 60 | + net_balances[receiver] += amount |
| 61 | + |
| 62 | + # Remove zero balances |
| 63 | + non_zero_balances = [amt for amt in net_balances.values() if amt != 0] |
| 64 | + number_of_non_zero_balances = len(non_zero_balances) |
| 65 | + |
| 66 | + if non_zero_balances == 0: |
| 67 | + return 0 |
| 68 | + |
| 69 | + def backtrack(start: int) -> int: |
| 70 | + # Skip settled accounts |
| 71 | + while start < number_of_non_zero_balances and non_zero_balances[start] == 0: |
| 72 | + start += 1 |
| 73 | + # All accounts settled |
| 74 | + if start >= number_of_non_zero_balances: |
| 75 | + return 0 |
| 76 | + |
| 77 | + min_transactions = float("inf") |
| 78 | + # Try to settle non_zero_balances[start] by paring with another opposite sign balance |
| 79 | + for j in range(start + 1, number_of_non_zero_balances): |
| 80 | + # One owes, the other is owed |
| 81 | + if non_zero_balances[start] * non_zero_balances[j] < 0: |
| 82 | + non_zero_balances[j] += non_zero_balances[start] |
| 83 | + # recurse for remaining transactions after settling this one |
| 84 | + min_transactions = min(min_transactions, 1 + backtrack(start + 1)) |
| 85 | + # backtrack, undo the settlement |
| 86 | + non_zero_balances[j] -= non_zero_balances[start] |
| 87 | + if non_zero_balances[j] + non_zero_balances[start] == 0: |
| 88 | + break |
| 89 | + return min_transactions |
| 90 | + |
| 91 | + return backtrack(0) |
0 commit comments