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 标题: ...
随机推荐
- java_day07_异常
第七章: 异常 1.异常概述 在我们日常生活中,有时会出现各种各样的异常,例如:职工小王开车去上班,在正常情况下,小王会准时到达单位.但是天有不测风云,在小王去上班时,可能会遇到一些异常情况,比如小王 ...
- 【2017-05-19】WebForm复合控件、用DropDownList实现时间日期选择。
自动提交的属性: AutoPostBack="True" 1.RadioButtonList 单选集合 -属性:RepeatDirection:Vertical (垂直排布 ...
- VIM技巧----改变
1.大小写转换 ~ 将光标下的字母改变大小写 vaw~ 选中单词(vaw:a会选择一个对象(an object)包括空格在内)后进行大小写转换 viw~ 选中单词(viw:i会选择一个对象的内部(an ...
- Linux 命令配置IP
配置静态IP:ip addr add 192.168.18.18/24 dev eth0 启动网卡:ifup eth0/ifup ifcfg-eth0 添加默认网关路由:ip route add de ...
- 集合(五) TreeMap
4.TreeMap SortedMap接口继承Map接口,是排序键值对的接口,实现排序的的方法是Comparator.而NavigableMap接口继承于SortedMap,新增了一些导航方法.而Tr ...
- HNOI2004 树的计数 和 HNOI2008 明明的烦恼
树的计数 输入文件第一行是一个正整数n,表示树有n个结点.第二行有n个数,第i个数表示di,即树的第i个结点的度数.其中1<=n<=150,输入数据保证满足条件的树不超过10^17个. 明 ...
- CodeFroces 758C - Unfair Poll
题意: 老师点名,顺序是1 -- n -- 1 排为一个循环,每列为1 -- m的顺序, 问点到最多次数和最少次数的人的次数以及(x,y)被点的次数. 分析: 由于点名有循环,故可先判断出每一个循环每 ...
- ON_WM_MOUSEWHEEL无响应
问题:ON_WM_MOUSEWHEEL消息无响应 转动滚轮会导致Windows在有输入焦点的窗口(不是鼠标光标下面的窗口)产生WM_MOUSEWHEEL消息.所以当子窗口没有焦点的时候将收不到消息WM ...
- php回顾(3)系统函数
abs() 绝对值 ceil() 向上取整 floor() 向下取整 round() 四舍五入 第二个参数:保留小数点后面几位 ...
- PHP回顾(2)
print_r()打印输出原格式,就加上标签<pre>.例子:echo '<pre>'; print_r($arr); echo '<pre>'; 添加数组的时候, ...