Skip to problem
←

Minimum Payments to Settle Debts

Hard 0 attempts

Given an N × N debt matrix graph, where graph[i][j] is the amount person i owes person j, return a settlement using the fewest individual payments.

Each payment is [payer, receiver, amount]. People are numbered from 0 to N - 1, and amounts must be positive integers. Payments may be made between any two different people, even if no original debt connects them. Only each person’s net position must be preserved: total money received minus total money paid must equal their original incoming debts minus outgoing debts.

Minimize the number of payments, not the total amount transferred. Any optimal settlement is accepted, in any order. If everyone’s incoming and outgoing debts cancel, return [].

For a chain where person 0 owes 1 five units and 1 owes 2 five units, a single payment [0,2,5] settles everyone’s net position. Cyclic debts may cancel without any payments.

Function contract

Implement minCashFlow(graph). graph[i][j] is the nonnegative integer amount person i owes person j; people use zero-based indices.

Output: Return an array of [payer, receiver, amount] payments with positive integer amounts. Preserve every person’s net balance using the minimum possible number of payments. Any optimal settlement and payment order are accepted; return [] if all net balances are zero.

Example 1

Input
graph = [[0,1000,2000],[0,0,5000],[0,0,0]]
Output
[[1,2,4000],[0,2,3000]]

Example 2

Input
graph = [[0,0],[0,0]]
Output
[]

Example 3

Input
graph = [[0,0,0,0,8,0,0],[0,0,0,0,1,6,0],[0,0,0,0,0,2,4],[0,0,0,0,0,0,5],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]
Output
[[0,5,8],[1,4,7],[2,4,2],[2,6,4],[3,6,5]]

Explanation: Net balances are [-8,-7,-6,-5,9,8,9]. Settle the matching 8-unit pair first, then settle the remaining five non-zero balances in four payments. Five payments are optimal; matching the largest debtor and creditor greedily needs six.

Constraints
  • 1 <= N <= 10
  • graph is an N × N matrix of non-negative integer debts
  • Each debt and every net balance are safe JavaScript integers.

Backtracking on Net Balances

First cancel each person's incoming and outgoing debts. Skip settled people, then pair the first unsettled balance with each opposite-sign balance. Transfer the first person's full remaining balance and recurse; the counterpart may become a debtor or creditor. Keep the settlement with the fewest payments. This exhaustive search includes an optimal settlement, unlike repeatedly matching the largest debtor and creditor.

Use BigInt for intermediate balances: accumulating several individually safe debts can temporarily exceed Number’s exact range before incoming and outgoing amounts cancel. Return payment amounts as numbers. They remain safe because each transfer combines opposite-sign balances, whose resulting magnitude cannot exceed the larger original magnitude.

javascript
function minCashFlow(graph) {
  const n = graph.length, net = Array(n).fill(0n);
  for (let i = 0; i < n; i++) for (let j = 0; j < n; j++) {
    net[i] -= BigInt(graph[i][j]); net[j] += BigInt(graph[i][j]);
  }
  let best = null;
  function search(start, payments) {
    while (start < n && net[start] === 0n) start++;
    if (start === n) { if (!best || payments.length < best.length) best = payments.slice(); return; }
    if (best && payments.length >= best.length) return;
    const balance = net[start], tried = new Set();
    for (let j = start + 1; j < n; j++) {
      if (net[j] * balance >= 0n || tried.has(net[j])) continue;
      tried.add(net[j]);
      const previous = net[j]; net[j] += balance;
      payments.push(balance < 0n ? [start, j, Number(-balance)] : [j, start, Number(balance)]);
      search(start + 1, payments);
      payments.pop(); net[j] = previous;
      if (previous + balance === 0n) break;
    }
  }
  search(0, []);
  return best || [];
}

Time: O(N² + N!) as a loose upper bound. Space: O(N²) auxiliary space in the worst case for the per-recursion-level tried sets, plus O(N) for balances and payments.

Loading discussion…

Saved in this browser only. Private to you.

JavaScript