一群朋友去度假,有时互相借钱。

例如,爱丽丝为比尔的午餐支付了 10 美元。后来克里斯给爱丽丝 5 美元搭出租车。我们可以假设每笔交易为一个三元组(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的更多相关文章

  1. Leetcode: Optimal Account Balancing

    A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...

  2. [LeetCode] Optimal Account Balancing 最优账户平衡

    A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...

  3. [LeetCode] 465. Optimal Account Balancing 最优账户平衡

    A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for ...

  4. 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 ...

  5. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  6. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  7. Hard模式题目

    先过一下Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Ha ...

  8. 继续过Hard题目

    接上一篇:http://www.cnblogs.com/charlesblc/p/6283064.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance ...

  9. 继续过Hard题目.0209

    http://www.cnblogs.com/charlesblc/p/6372971.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Diffi ...

  10. 继续过Hard题目.0207

    接上一篇:http://www.cnblogs.com/charlesblc/p/6364102.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance ...

随机推荐

  1. Educational Codeforces Round 48 (Rated for Div. 2)

    http://codeforces.com/contest/1016 A. 没想到这个也会TLE,太粗心了 B. 暴力就好了,多情况讨论又出错... 思路跟我一样的解法   为什么我做了那么多讨论,原 ...

  2. 论文笔记:Prediction-Tracking-Segmentation

    Prediction-Tracking-Segmentation 2019-04-09 18:47:30 Paper:https://arxiv.org/pdf/1904.03280.pdf 之所以要 ...

  3. 信息安全之路-web-xss学习(2)

    存储型xss漏洞 该漏洞会将执行命令写入数据库,每一个用户在该页面停留时,都会被动执行该js命令,从而被盗取cookie 1.DVWA平台验证-low等级 <?php if( isset( $_ ...

  4. 容器中的诊断与分析3——live diagnosis——lldb

    windows下,我们对于.net程序发生Crash,资源泄露,死锁等问题的分析,有神器windbg .net core程序运行在linux上时,该怎么进行对对Core Dump文件进行分析呢?今天介 ...

  5. java - 策略模式、状态模式、卫语句,避免多重if-else(转)

    前言 当代码中出现多重if-else语句或者switch语句时.弊端之一:如果这样的代码出现在多处,那么一旦出现需求变更,就需要把所有地方的if-else或者switch代码进行更改,要是遗漏了某一处 ...

  6. vsphere client/web client 开启ESXi SSH服务

    ●vsphere client 开启ESXi SSH服务 1,从主机和群集列表选择主机esxi01.2,选择[配置]-->[软件]列表-->[安全配置文件]--> [服务],点击右上 ...

  7. Windows Server 2012 R2 英文版安装中文语言包教程

    Windows Server 是云操作系统的主要组成部分. 有了 Windows Server,再加上云操作系统内的开发者技术,您就可以构建现代业务应用程序. 现代业务应用程序通常涵盖内部部署资源和公 ...

  8. springboot 启动报错

    有一个警告 :** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the d ...

  9. Cache Line

    转载: https://yq.aliyun.com/articles/46550

  10. mybatis 动态sql和参数

    mybatis 动态sql 名词解析 OGNL表达式 OGNL,全称为Object-Graph Navigation Language,它是一个功能强大的表达式语言,用来获取和设置Java对象的属性, ...