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

例如,爱丽丝为比尔的午餐支付了 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. Bytom储蓄分红合约解析

    储蓄分红合约简介 储蓄分红合约指的是项目方发起了一个锁仓计划(即储蓄合约和取现合约),用户可以在准备期自由选择锁仓金额参与该计划,等到锁仓到期之后还可以自动获取锁仓的利润.用户可以在准备期内(dueB ...

  2. 解决 EDAS:Upload failed: The right margin is 0.535 in on page 1 问题

    参考: IEEETran page margins 解决 EDAS:Upload failed: The right margin is 0.535 in on page 1 问题 在 EDAS 上上 ...

  3. yii restful和一般路由共存

    <?php namespace app\controllers; use Yii; use yii\rest\ActiveController; /** * */ class TestContr ...

  4. ES6多层解构

    const info = { person: { name: 'xiaobe', other: { age: 22, } }, song: 'rolling', } // 解构person的内容 co ...

  5. ArrayList源码阅读笔记(1.8)

    目录 ArrayList类的注解阅读 ArrayList类的定义 属性的定义 ArrayList构造器 核心方法 普通方法 迭代器(iterator&ListIterator)实现 最后声明 ...

  6. 蚂蚁金服mPaaS 3.0发布 助力客户智能化构建超级App生态

    1月4日,蚂蚁金融科技宣布蚂蚁金服移动开发平台mPaaS(mobile Platform-as-a-Service)升级到3.0版本,“新版本以智能技术助力客户构建自己的超级 App,企业可以拥有等同 ...

  7. 深入理解Java虚拟机(第二版)中《长期存活对象将进入老年代》的实践

  8. julia 安装

    https://julialang.org/downloads/ 下载安装程序,拖拽完成安装

  9. 一个可以配置阴影方向和颜色的类 CardView 控件 SCardView

    一.控件简述 今天给大家推荐一个控件 SCardView ,看名字就很容易才出来它其实就是一个 CardView .把它拿出来,是因为它解决了一些 CardView 无法实现的需求以及简化了 Card ...

  10. spring RedisTemplate的使用(一)--xml配置或JavaConfig配置

    1.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="h ...