Formatted question description: https://leetcode.ca/all/465.html

465. Optimal Account Balancing

Level

Hard

Description

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:

  1. A transaction will be given as a tuple (x, y, z). Note that x ≠ y and z > 0.
  2. 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.

Example 1:

Input:

[[0,1,10], [2,0,5]]

Output:

2

Explanation:

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:

Input:

[[0,1,10], [1,0,1], [1,2,5], [2,0,5]]

Output:

1

Explanation:

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.

Solution

First loop over transactions and obtain each person’s remaining amount of money (either 0, positive or negative). Then do backtrack.

Starting from index 0 and transaction count 0, loop over each remaining amount after the start index. If a next amount has a different sign with the amount at the start index, then add the amount of the start index by the current amount with the number of transactions increased by 1, and do backtrack using the current index as the start index. If the end is reached, update the minimum number of transactions. Finally, return the minimum number of transactions.

class Solution {
    int minTransactions = Integer.MAX_VALUE;

    public int minTransfers(int[][] transactions) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int[] transaction : transactions) {
            int person1 = transaction[0], person2 = transaction[1], amount = transaction[2];
            int value1 = map.getOrDefault(person1, 0) - amount;
            int value2 = map.getOrDefault(person2, 0) + amount;
            map.put(person1, value1);
            map.put(person2, value2);
        }
        int groupSize = map.size();
        int[] balances = new int[groupSize];
        Set<Integer> keySet = map.keySet();
        int index = 0;
        for (int key : keySet) {
            int balance = map.get(key);
            balances[index++] = balance;
        }
        backtrack(balances, 0, 0);
        return minTransactions;
    }

    public void backtrack(int[] balances, int start, int count) {
        int length = balances.length;
        while (start < length && balances[start] == 0)
            start++;
        if (start == length)
            minTransactions = Math.min(minTransactions, count);
        else {
            boolean positive = balances[start] > 0;
            for (int i = start + 1; i < length; i++) {
                if ((balances[i] > 0) ^ positive) {
                    balances[i] += balances[start];
                    backtrack(balances, start + 1, count + 1);
                    balances[i] -= balances[start];
                }
            }
        }
    }
}

All Problems

All Solutions