465. Optimal Account Balancing (Hard)

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 1: Hash Table 9ms

我们使用一个哈希表来建立每个人和其账户的映射,其中账户若为正数,说明其他人欠你钱;如果账户为负数,说明你欠别人钱。我们对于每份账单,前面的人就在哈希表中减去钱数,后面的人在哈希表中加上钱数。这样我们每个人就都有一个账户了,然后我们接下来要做的就是合并账户,看最少需要多少次汇款。我们先统计出账户值不为0的人数,因为如果为0了,表明你既不欠别人钱,别人也不欠你钱,如果不为0,我们把钱数放入一个数组accnt中,然后调用递归函数。在递归函数中,我们初始化结果res为整型最大值,然后我们跳过为0的账户,然后我们开始遍历之后的账户,如果当前账户和之前账户的钱数正负不同的话,我们将前一个账户的钱数加到当前账户上,这很好理解,比如前一个账户钱数是-5,表示张三欠了别人五块钱,当前账户钱数是5,表示某人欠了李四五块钱,那么张三给李四五块,这两人的账户就都清零了。然后我们调用递归函数,此时从当前改变过的账户开始找,num表示当前的转账数,需要加1,然后我们用这个递归函数返回的结果来更新res,后面别忘了复原当前账户的值。遍历结束后,我们看res的值如果还是整型的最大值,说明没有改变过,我们返回num,否则返回res即可,参见代码如下:

检查当前交易次数,如果大于之前的结果,之前返回,速度会快很多。

class Solution {
    void dfs(vector<int>& accnt, int start, int cnt, int& res) {
        if (cnt >= res) return;
        int n = accnt.size();
        // current account has been settled up
        while (start < n && accnt[start] == 0) ++start;
        if (start == n) res = cnt;
        // try to settle up account[start]
        for (int i = start+1; i < n; ++i) {
            if ((accnt[start] > 0 && accnt[i] < 0) || (accnt[start] < 0 && accnt[i] > 0)) {
                // settle accout start
                accnt[i] += accnt[start]; 
                // recurion to settle next account
                dfs(accnt, start+1, cnt+1, res);
                accnt[i] -= accnt[start];
            }
        }
    }
public:
    int minTransfers(vector<vector<int>>& transactions) {
        unordered_map<int, int> acc;
        for (auto& trans: transactions) {
            acc[trans[0]] -= trans[2];
            acc[trans[1]] += trans[2];
        }
        vector<int> accnt;
        for (auto x : acc) {
            // remove settled up accounts
            if (x.second != 0) accnt.push_back(x.second);
        }
        if (accnt.size() == 0) return 0;
        int res = accnt.size()-1;
        dfs(accnt, 0, 0, res);
        return res;
    }
};

Solution 2: Hash Table 3ms

class Solution {
public:
    int minTransfers(vector<vector<int>>& transactions) {
        unordered_map<int, int> mp;
        for (auto x : transactions) {
            mp[x[0]] -= x[2];
            mp[x[1]] += x[2];
        }
        vector<int> in;
        vector<int> out;
        for (auto x : mp) {
            if (x.second < 0) out.push_back(x.second);
            else if (x.second > 0) in.push_back(x.second);
        }
        // maximum possible transcations
        int res = in.size()+out.size()-1; 
        dfs(in, out, 0, 0, res);
        return res;
    }

    void dfs(vector<int> &in, vector<int> &out, int s1, int s2, int cnt, int& res) {
        if (cnt > res) return;
        int res = INT_MAX, n = in.size();
        while (s1 < n && in[s1] == 0) ++s1;
        if (s1 == n) return 0;

        for (int i = s2; i < out.size(); ++i) {

        }

        return res;
    }
};

results matching ""

    No results matching ""