optimal-account-balancing
一群朋友去度假,有时互相借钱。
例如,爱丽丝为比尔的午餐支付了 10 美元。后来克里斯给爱丽丝 5 美元搭出租车。我们可以假设每笔交易为一个三元组(X,Y,Z),这意味着第 X 个人借给第 Y 个人 Z 美元。假设 Alice,Bill 和 Chris 是第0,1,2 个人(0,1,2是他们的ID),他们之间的交易可以表示为[ [ 0,1,10 ],[ 2,0,5 ] ]。
给定一组人之间的交易清单,返回结算所需的最低交易数量。
九章算法一篇置顶题目 挺有意思 第一次看到这种需要指数级复杂度的面试题
下面的解法非常具有局限性 如果 debt非零的人多于32 或 64 下面的方法是行不通的
public class Solution {
public int minTransfer(int[][] transactions){
Map<Integer, Integer> debts = new HashMap<Integer, Integer>();
for(int[] t: transactions){
debts.put(t[0], getOrDefault(t[0], 0)-t[2]);
debts.put(t[1], getOrDefault(t[1], 0)+t[2]);
}
int len = 0;
int[] account = new int[debts.size()];
for(int total: debts.values()){
if(total!=0){
account[len++] = total;
}
}
if(len ==0) return 0;
int dp = new int[1<<len];
Arrays.fill(dp, Integer.MAX_VALUE);
for(int l =1; l <dp.length; l++){
int sum =0;
int count =0;
for(int i=0; i<len;i++){
if((1<<i&l)!=0){
sum + = account[i];
count ++;
}
}
if(sum ==0){
dp[l] = count-1;
for(int i =1; i<l;i++){
if((i&l)==i &&(dp[i]+dp[l-i])< dp[l]){
dp[l] = dp[i]+dp[l-i];
}
}
}
}
return dp[len-1];
}
}
optimal-account-balancing的更多相关文章
- Leetcode: Optimal Account Balancing
A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...
- [LeetCode] Optimal Account Balancing 最优账户平衡
A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...
- [LeetCode] 465. Optimal Account Balancing 最优账户平衡
A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...
- 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 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- Hard模式题目
先过一下Hard模式的题目吧. # Title Editorial Acceptance Difficulty Frequency . 65 Valid Number 12.6% Ha ...
- 继续过Hard题目
接上一篇:http://www.cnblogs.com/charlesblc/p/6283064.html 继续过Hard模式的题目吧. # Title Editorial Acceptance ...
- 继续过Hard题目.0209
http://www.cnblogs.com/charlesblc/p/6372971.html 继续过Hard模式的题目吧. # Title Editorial Acceptance Diffi ...
- 继续过Hard题目.0207
接上一篇:http://www.cnblogs.com/charlesblc/p/6364102.html 继续过Hard模式的题目吧. # Title Editorial Acceptance ...
随机推荐
- Spring 学习——Spring AOP——AOP配置篇Advice(无参数传递)
声明通知Advice 配置方式(以前置通知为例子) 方式一 <aop:config> <aop:aspect id="ikAspectAop" ref=" ...
- 让selenium中的Cromerderive不加载图片设置
把配置参数(chrom_opt)设置好后将其添加到 browser = webdriver.Chrome(executable_path="chromedriver.exe的路径" ...
- Unable to start Ocelot because either a ReRoute or GlobalConfiguration
在ASP.Net Core项目APIGateway中添加Ocelot+Consul然后运行时 ,VS2017报如下错 : 内部异常 1: Exception: Unable to start Ocel ...
- Vivado HLS 工具
干什么的 Vivado HLS工具可以将C语言高级综合为硬件. 为什么要使用HLS 可以在更高的抽象层次描述功能,而不是在传统的RTL级别 一个潜在的用处是,系统设计划分成硬件部分和软件部分之后,软件 ...
- Lintcode177-Convert Sorted Array to Binary Search Tree With Minimal Height-Easy
177. Convert Sorted Array to Binary Search Tree With Minimal Height Given a sorted (increasing order ...
- 剑指offer:链表中倒数第k个结点
问题描述 输入一个链表,输出该链表中倒数第k个结点. 解题思路 两个指针都指向头结点,第一个指针先移动k-1个结点,之后两指针同时移动,当第一个指针到链表尾的时候,第二个指针刚好指向倒数第k个结点. ...
- 做数据挖掘,就算发 20 几分的 CNS 子刊,也是垃圾!?--转载
关于数据挖掘发表文章,我们知道很多人是看不上.瞧不起.嗤之以鼻的.大抵是因为这些人平时只发 CNS 主刊,所以才认为通过数据挖掘这种用「别人的数据」或者叫「干实验」来发文章是“「垃圾」,没有什么价值. ...
- 线程demo异常处理
今天写了个线程小demo,出现了异常, 如下: Traceback (most recent call last): File "threading.py", line 1, in ...
- Ehcache计算Java对象内存大小
在EHCache中,可以设置maxBytesLocalHeap.maxBytesLocalOffHeap.maxBytesLocalDisk值,以控制Cache占用的内存.磁盘的大小(注:这里Off ...
- Cocos Creator学习二:查找节点和查找组件
1.目的:只有通过方便的获取节点对象以及组件,才能较好的进行逻辑控制. 2.通过 cc.find(节点全路径名称字符串) 获取节点. 3.通过getComponent获取组件(注意一个是类型,一个是类 ...