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 ...
随机推荐
- mybatis配置与使用
一:Mybatis简介 1.名称由来 Mybatis原名叫ibatis.Mybatis一开始属于Apache,2010年从Apache转移到了GoogleCode这个组织中. 2.Mybatis概念 ...
- Python下关于RSA解密模块的使用
最近筹备一场CTF比赛发现了一道关于RSA解密的题如下: #小明得到了一个 RSA 加密信息,你能帮他解开吗? n = 4106906565495961459775020773869808579876 ...
- 解决 Cannot uninstall 'pyparsing' 问题
参考 pyparsing 无法卸载导致安装 matplotlib 出错 解决 Cannot uninstall 'pyparsing' 问题 在安装 pydot 时遇到依赖 pyparsing 无法更 ...
- springmvc学习之jdk版本,tomcat版本,spring版本
使用的软件是myeclipse2018,jdk8,tomcat9.0,spring3.2.0 以上为我的软件及各种配置 1.建立了web工程,build path 使用的是默认的j2EE1.8(只有配 ...
- ArrayList源码阅读笔记(1.8)
目录 ArrayList类的注解阅读 ArrayList类的定义 属性的定义 ArrayList构造器 核心方法 普通方法 迭代器(iterator&ListIterator)实现 最后声明 ...
- js 克隆数组
js克隆数组 1.遍历push 2.slice const a1 = [1, 2];const a2 = a1.slice() 3.concat() const a2 = a1.concat(); a ...
- java native用法
说明: native关键字说明其修饰的方法是一个原生态方法,方法对应的实现不是在当前文件,而是在用其他语言(如C和C++)实现的文件中.Java语言本身不能对操作系统底层进行访问和操作,但是可以通过J ...
- 博客搬家一下到CSDN
博客搬家一下到CSDN:http://blog.csdn.net/weixin_33409246
- Effective java 系列之更优雅的关闭资源-try-with-resources
背景: 在Java编程过程中,如果打开了外部资源(文件.数据库连接.网络连接等),我们必须在这些外部资源使用完毕后,手动关闭它们.因为外部资源不由JVM管理,无法享用JVM的垃圾回收机制,如果我们不在 ...
- 现代 PHP 新特性 —— 生成器入门(转)
原文链接:blog.phpzendo.com PHP 在 5.5 版本中引入了「生成器(Generator)」特性,不过这个特性并没有引起人们的注意.在官方的 从 PHP 5.4.x 迁移到 PHP ...