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.

这道题给了一堆某人欠某人多少钱这样的账单,问经过优化后最少还剩几个。其实就相当于一堆人出去玩,某些人可能帮另一些人垫付过花费,最后结算总花费的时候可能你欠着别人的钱,其他人可能也欠你的欠,需要找出简单的方法把所有欠账都还清就行了。这道题的思路跟之前那道 Evaluate Division 有些像,都需要对一组数据颠倒顺序处理。这里使用一个 HashMap 来建立每个人和其账户的映射,其中账户若为正数,说明其他人欠你钱;如果账户为负数,说明你欠别人钱。对于每份账单,前面的人就在 HashMap 中减去钱数,后面的人在哈希表中加上钱数。这样每个人就都有一个账户了,接下来要做的就是合并账户,看最少需要多少次汇款。先统计出账户值不为0的人数,因为如果为0了,表明你既不欠别人钱,别人也不欠你钱,如果不为0,把钱数放入一个数组 accnt 中,然后调用递归函数。在递归函数中,首先跳过为0的账户,然后看若此时 start 已经是 accnt 数组的长度了,说明所有的账户已经检测完了,用 cnt 来更新结果 res。否则就开始遍历之后的账户,如果当前账户和之前账户的钱数正负不同的话,将前一个账户的钱数加到当前账户上,这很好理解,比如前一个账户钱数是 -5,表示张三欠了别人5块钱,当前账户钱数是5,表示某人欠了李四5块钱,那么张三给李四5块,这两人的账户就都清零了。然后调用递归函数,此时从当前改变过的账户开始找,cnt 表示当前的转账数,需要加1,后面别忘了复原当前账户的值,典型的递归写法,参见代码如下:

class Solution {
public:
int minTransfers(vector<vector<int>>& transactions) {
int res = INT_MAX;
unordered_map<int, int> m;
for (auto t : transactions) {
m[t[]] -= t[];
m[t[]] += t[];
}
vector<int> accnt;
for (auto a : m) {
if (a.second != ) accnt.push_back(a.second);
}
helper(accnt, , , res);
return res;
}
void helper(vector<int>& accnt, int start, int cnt, int& res) {
int n = accnt.size();
while (start < n && accnt[start] == ) ++start;
if (start == n) {
res = min(res, cnt);
return;
}
for (int i = start + ; i < n; ++i) {
if ((accnt[i] < && accnt[start] > ) || (accnt[i] > && accnt[start] < )) {
accnt[i] += accnt[start];
helper(accnt, start + , cnt + , res);
accnt[i] -= accnt[start];
}
}
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/465

类似题目:

Evaluate Division

参考资料:

https://leetcode.com/problems/optimal-account-balancing/

https://leetcode.com/problems/optimal-account-balancing/discuss/95369/share-my-on-npc-solution-tle-for-large-case

https://leetcode.com/problems/optimal-account-balancing/discuss/95355/11-liner-9ms-DFS-solution-(detailed-explanation)

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 465. Optimal Account Balancing 最优账户平衡的更多相关文章

  1. [LeetCode] Optimal Account Balancing 最优账户平衡

    A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...

  2. LC 465. Optimal Account Balancing 【lock,hard】

    A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...

  3. Leetcode: Optimal Account Balancing

    A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...

  4. Optimal Flexible Architecture(最优灵活架构)

    来自:Oracle® Database Installation Guide 12_c_ Release 1 (12.1) for Linux Oracle base目录命名规范: /pm/s/u 例 ...

  5. OBST(Optimal Binary Tree最优二叉搜索树)

    二叉搜索树 二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的 ...

  6. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  7. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  8. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  9. Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

    All LeetCode Questions List 题目汇总 Sorted by frequency of problems that appear in real interviews. Las ...

随机推荐

  1. 海边拾贝-B-优秀博客/网站

    记下若干优秀博客,方便后期检索.会不定期更新: 优秀的程序员,从使用Github开始:https://help.github.com/en/github/managing-your-work-on-g ...

  2. Linux SELinux 介绍详解

    Linux SELinux 介绍详解 SElinux 简介 SElinux (Security Enhanced Linux)是由美国国家安全局(NSA)开发的.它已被植入到了Linux系统的内核当中 ...

  3. 『公交线路 状压dp 矩阵乘法加速』

    公交线路 Description 小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依次编号为1到N,相邻公交车站间的距离均为1km. 作为公交车线路的规划者,小Z调查了市民的 ...

  4. f(n-1) + f(n-2)的编译器处理

    https://gcc.godbolt.org   int addx(int a){ return a + 2; } int gooo(){ return addx(3) + addx(4) + ad ...

  5. kali渗透综合靶机(二)--bulldog靶机

    kali渗透综合靶机(二)--bulldog靶机 靶机下载地址:https://download.vulnhub.com/bulldog/bulldog.ova 一.主机发现 netdiscover ...

  6. python3模块

    一.sys模块 import sys #print(sys.path) #打印环境变量 #print(sys.argv) print(sys.argv[3]) Sys.argv[ ]其实就是一个列表, ...

  7. Eureka服务下线源码解析

    我们知道,在Eureka中,可以使用如下方法使Eureka主动下线,那么本篇文章就来分析一下子这个下线的流程 public synchronized void shutdown() { if (isS ...

  8. php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpos

    php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpossubstr_count($haystack, $needle [,$o ...

  9. js 设计模式——代理模式

    代理模式 代理模式是为一个对象提供一个代用品或占位符,以便控制对它的访问. 生活中有很多的代理模式的场景.例如,明星有经纪人作为代理,老板有秘书作为代理等等,当有事情的时候,会找到经纪人或秘书,再由他 ...

  10. SharpGL之透视投影和摄像机

    当三维体放在世界坐标系中后,由于显示器只能用二维图像显示三维休,因此必须要依赖投影来把三维体降低维数. 投影变换的目的就是定义了一个视景体,使得视景体外多余的部分不会显示. 投影包括透视投影(pers ...