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 标题: ...
随机推荐
- python 匿名函数lambda使用
lambda函数语法格式: lambda函数 后面参数可以有一个或多个,冒号后面是python表达式: lambda 参数1,参数2,参数3...:表达式 # 一个参数情况: a = lambda x ...
- php打包下载以及断点续传
php下载单文件 以及 多文件打包下载,支持断点续传 断点续传的功能未经验证 需要nginx或者apache服务器指定静态文件,png, mp4, zip等后缀文件的目录, 直接实例化并调用 down ...
- 【Day2】2.函数
视频地址(全部) https://edu.csdn.net/course/detail/26057 课件地址(全部) https://download.csdn.net/download/gentl ...
- Selenium(4)
练习1:使用selenium+firefox测试ecshop登录过程 一.WebDriver 1.启动浏览器 (1)启动Firefox浏览器 a.启动默认路径下的浏览器 WebDriver drive ...
- 数据库——Oracle(4)
1 Oracle中常用字符处理函数:用来处理char,varchar以及varchar2类型数据. 1)length(列名/字符串):统计当前该列的列值/字符串中字符的个数 select ename, ...
- webpack拷贝插件 copy-webpack-plugin
copy-webpack-plugin 安装 npm install --save-dev copy-webpack-plugin 作用:在webpack中拷贝文件和文件夹 from 定义要拷贝的源文 ...
- curses is not supported on this machine:(curses 在pycharm(Windows)中的安装 )
curse在Windows下的pycharm中安装,curse是不能直接在Windows下跑的.需要安装相关环境,要根据直接project的编译器版本来选择下载相关的whl. 找到project的Sc ...
- mongodb cursor用法
为了营造大批量数据,我们可以这样写javascript脚本 for (var i=1;i<=10000;i++) { if(i%2==1) { db.cursortest.insert({_id ...
- 【leetcode】1286. Iterator for Combination
题目如下: Design an Iterator class, which has: A constructor that takes a string characters of sorted di ...
- C# 常用方法——base64字符串转图片
其他常用方法详见:https://www.cnblogs.com/zhuanjiao/p/12060937.html /// <summary> /// base64编码的文本转为图片 / ...