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 ≠ yandz > 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 标题: ...
随机推荐
- 使用ViewFlipper实现图片轮播
public class MainActivity extends AppCompatActivity { private ViewFlipper flipper; //背景图片int[] id pr ...
- C#形参和实参、引用类型和值类型使用时的一个注意点。
这是早上群里讨论的例子. static void main(string [] arg){ var p1=new Person{Name="张三"}; var p2=new Per ...
- SQL Server中四类事务并发问题的实例再现(转)
本篇文章将用实例再现数据库访问中四类并发问题,希望能让初学者能对事务的并行性有进一步的理解. 首先,让我们先来了解一下并行问题以及事务隔离级别这两个概念.在数据库中,假设如果没有锁定且多个用户同时访问 ...
- 安装WIN10+Ubuntu18.04安装教程(实测有效)
转载原文链接:https://www.cnblogs.com/masbay/articles/10745170.html 安装过程中尤其注意分区时候的挂载点一定要选对!!!选择Ubuntu的EFI所在 ...
- Trie树(代码),后缀树(代码)
Trie树系列 Trie字典树 压缩的Trie 后缀树Suffix tree 后缀树--ukkonen算法 Trie是通过对字符串进行预先处理,达到加快搜索速度的算法.即把文本中的字符串转换为树结构, ...
- 使用CreateWindowEx创建子窗口的注意事项
比如: 使用 HWND child = CreateWindowEx(0,L"childclass",NULL,WS_CHILD | WS_VISIBLE | WS_CLIPSIB ...
- 王某的NLP之路前言
感谢基友jayjay和海英学姐的指路,其实我的方向一直比较迷茫. 因为自己是会计学出身的,前三年也没接触编程,第一次接触还是在2016年,尝试用聚宽的量化接口,当时顺便学了python 的一点知识. ...
- Web前端开发——HTML文件结构
在编写html文件时,把文件保存成 .htm 或 .html的后缀. 基本文件结构 <html> <head> <title></title> < ...
- Error from server (ServiceUnavailable): the server is currently unable to handle the request
grep image /root/kubernetes-metrics-server/kubernetes-metrics-server/metrics-server-deployment.yaml ...
- windows环境下,mysql的root密码丢失后重置方法
运行窗口输入 services.msc,检查mysql服务是否启动,如果启动手动停止或输入 net stop mysql 停止msyql服务. 打开cmd命令行,使用cd命令进入mysql 的bi ...