465. Optimal Account Balancing

465. Optimal Account Balancing


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:

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

方法1: dfs

思路:

首先画个图,可以发现最终的目的是使图上的所有人都收支平衡。所以对于那些经过当前所有交易已经收支平衡的人可以不用被考虑在图里面了。由于题目还说了id可以是任何数,我们选择首先用unordered_map来记录每个人的balance,但是hash可以被随即转换成vector,因为id绝对值变得不再重要,可以用vector中的相对位置来替代。那么vector bal中剩下的就是所有负债或借债的人。如何达到平衡呢?比如id = 0的人欠了bal[0] 这么多钱,他只要把bal[0]的钱再还出去就好了,具体优先还给谁才能使得交易量最小?这就需要用backtracking来尝试了。所以我们得知bal[0]之后就寻找和bal[0]符号相反的人bal[i],找到直接把钱还给 i 使得bal[0] = 。注意这里只强行保证bal[0]为0,但bal[i]仍可能有债务或者过剩,但是剩下的交给下一层backtracking去做就行了。此时bal[0]就可以被drop掉,我们继续对后面还未平衡的继续发起dfs,交易量+1,start + 1。当然这里的前提是最后的负债一定可以清零,这一点因为我们已知所有的交易关系,所以可以保证。另外在开始遍历之前要跳过那些已经被前面清零的人,如果已经遍历结束,才可以更新全球变量(order is tricky)。

Complexity

Time complexity: ???
Space complexity: ???

class Solution {
public:
    int minTransfers(vector<vector<int>>& transactions) {
        unordered_map<int, int> hash;
        vector<int> bal;
        int result = INT_MAX;
        for (auto a: transactions) {
            hash[a[0]] += a[2];
            hash[a[1]] -= a[2];
        }
        for (auto a: hash) {
            if (a.second) bal.push_back(a.second);
        }
        transHelper(bal, 0, result, 0);
        return result;
    }

    void transHelper(vector<int> & bal, int start, int & result, int current){
        while (start < bal.size() && bal[start] == 0) start++; 
        if (start == bal.size()) {
            result = min(current, result);
            return;
        }
        for (int i = start + 1; i < bal.size(); i++) {
            if (bal[start] * bal[i] < 0) {
                bal[i] += bal[start];
                transHelper(bal, start + 1, result, current + 1);
                bal[i] -= bal[start];
            }
        }
        return;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值