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 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
andz > 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. Runtime 24 ms, faster than 30.39% 这题本来以为是图,结果是数组的题。先建立每一个人的一个账户,然后DFS,DFS的时候从0-n,把第i个账户的钱都转到某一个j,这两个账户的金额是相反的,
因为只有相反的才会有交易。
class Solution {
public:
int minTransfers(vector<vector<int>>& transactions) {
map<int,int> m;
for(auto t: transactions){
m[t[]] -= t[];
m[t[]] += t[];
}
vector<int> accnt(m.size());
int cnt = ;
for(auto a : m){
if(a.second != ) accnt[cnt++] = a.second;
}
return helper(accnt, , cnt, );
}
int helper(vector<int>& accnt, int start, int n, int num){
int ret = INT_MAX;
while(start < n && accnt[start] == ) start++;
for(int i=start+; i<n; i++){
if((accnt[start] < && accnt[i] > ) || (accnt[start] > && accnt[i] < )){
accnt[i] += accnt[start];//加入第i个账户中,这个时候start账户已经没有钱了,可以进行下一个账户清理了
ret = min(ret, helper(accnt,start+, n, num+));//DFS,保存最小值
accnt[i] -= accnt[start];
}
}
return ret == INT_MAX ? num : ret;
}
};
LC 465. Optimal Account Balancing 【lock,hard】的更多相关文章
- LC 727. Minimum Window Subsequence 【lock,hard】
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
- LC 683. K Empty Slots 【lock,hard】
There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one ...
- [LeetCode] 465. Optimal Account Balancing 最优账户平衡
A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...
- LC 425. Word Squares 【lock,hard】
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- LC 774. Minimize Max Distance to Gas Station 【lock,hard】
On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...
- LC 272. Closest Binary Search Tree Value II 【lock,hard】
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- LC 644. Maximum Average Subarray II 【lock,hard】
Given an array consisting of n integers, find the contiguous subarray whose length is greater than o ...
- LC 431. Encode N-ary Tree to Binary Tree 【lock,hard】
Design an algorithm to encode an N-ary tree into a binary tree and decode the binary tree to get the ...
- 【深圳,武汉】一加科技(One Plus)招聘,寻找不...
[深圳,武汉]一加科技(One Plus)招聘,寻找不... [深圳,武汉]一加科技(One Plus)招聘,寻找不... 来自: 一加 2013-12-30 15:28:04 标题: ...
随机推荐
- 网络基础篇之HDLC、PPP(原理)
一.广域网传输 之前讲解的都是关于局域网的数据传输,这次讲解的是广域网的传输. 广域网简称WAN,是一种跨越超大的.地域性的计算机网络集合.通常跨省.市.甚至一个国家.广域网包括很多子网,子网可以是局 ...
- js基本事件
1:单击事件 --onclick function test01(){ alert("js的单击事件"); } 2:双击事件--ondblclick function test02 ...
- shell命令学习记录
id id会显示用户以及所属群组的实际与有效ID hostname 用来显示或者设置主机名(show or set the system’s host name).环境变量HOSTNAME也保存了当前 ...
- Python学习笔记----数据类型 运算符 循环 条件判断
1. Python安装 在官网www.python.org下载安装程序,可以支持的操作系统linux,windows,mac. Python版本:2.x和3.x,分别有x86和x64. 在Window ...
- 使用curl出现,curl: /usr/local/lib/libssl.so.1.1: version `OPENSSL_1_1_1' not found (required by /usr/lib/x86_64-linux-gnu/libcurl.so.4)
主要原因是curl找不到openssl的路径,所以只要将openssl的路径添加到相应的变量中就可以了. 参考连接https://blog.csdn.net/RookieWutongshu/artic ...
- 集成 jpush
给 iOS 应用添加推送功能是一件比较麻烦的事情,本篇文章收集了集成 jpush-react-native 的常见问题,目的是为了帮助用户更好地排查问题 1. 收不到推送 确保是在真机上测试,而不是在 ...
- 解决nginx无法访问的问题
解决nginx无法访问的问题 解决方案-->恢复Nginx默认配置: cd /usr/local/nginx/conf rm nginx.conf cp nginx.conf.default n ...
- python的方法VSjava方法
java 类方法和实例方法 类方法 用static修饰的方法. 由于类方法是属于整个类的,所以类方法的方法体中不能有与类的对象有关的内容. 即类方法体有如下限制: 1.类方法中不能引用对象变量: 2. ...
- ant-design-vue 报错 ReferenceError: h is not defined
使用表格,在配置 columns时用到了 customRender,然后就报错了 <script> import FileName from '@/views/admin/document ...
- css-滚动条常用的样式修改
// ::-webkit-scrollbar { /*滚动条整体样式*/// width: 10px; /*宽分别对应竖滚动条的尺寸*/// height: 5px; /*高分别对应横滚动条的尺寸*/ ...