Leetcode: Optimal Account Balancing
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 and z > 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. Show Company Tags
Backtracking: time complexity O(N!)
Use HashMap to store the initial debts of each person, negative means the person sends money to others, positive means the person gets money from others.
now if the map value is 0, which means the person is all set, free of debts.
Only consider those people with debts(either positive or negative)
store them in an array, use backtracking and greedy to clear each person's debts from 1st person till last one.
How to clear one person's debt? find a person 2 in the following array that has opposite sign(+->- or - -> +), and clear person1's whole debt with person2 only.
Here's the trick: example: [7, -6, -1], one obvious optimal solution is person1 pay $6 to person2, and pay $1 to person3. Notice that this optimal solution is equivalent to another solution:
person1 pay $7 to person2, and person2 pay $1 to person3. So when doing DFS, everytime we only consider clearing person1's debt wholly with another 1 person, we don't need to consider clearing with other more people, cause clearing with 1 person is already guaranteed to be optimal.
This problem still has some debates in discussion, will check later
public class Solution {
int res = Integer.MAX_VALUE;
public int minTransfers(int[][] transactions) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int[] transaction : transactions) {
map.put(transaction[0], map.getOrDefault(transaction[0], 0)-transaction[2]);
map.put(transaction[1], map.getOrDefault(transaction[1], 0)+transaction[2]);
}
ArrayList<Integer> depts = new ArrayList<Integer>();
for (int dept : map.values()) {
if (dept != 0) depts.add(dept);
}
helper(depts, 0, 0);
return res;
}
public void helper(ArrayList<Integer> depts, int start, int count) {
while (start<depts.size() && depts.get(start)==0) start++;
if (start == depts.size()) {
res = Math.min(res, count);
return;
}
for (int i=start+1; i<depts.size(); i++) {
if (depts.get(start)<0&&depts.get(i)>0 || depts.get(start)>0&&depts.get(i)<0) {
depts.set(i, depts.get(i)+depts.get(start));
//int store = depts.get(start);
//depts.set(start, 0);
helper(depts, start+1, count+1);
//depts.set(start, store);
depts.set(i, depts.get(i)-depts.get(start));
}
}
}
}
Leetcode: 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] 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] Optimal Division 最优分隔
Given a list of positive integers, the adjacent integers will perform the float division. For exampl ...
- LeetCode Optimal Division
原题链接在这里:https://leetcode.com/problems/optimal-division/description/ 题目: Given a list of positive int ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)
All LeetCode Questions List 题目汇总 Sorted by frequency of problems that appear in real interviews. Las ...
随机推荐
- jface databinding:部分实现POJO对象的监测
在前一篇博文<jface databinding/PojoBindable实现对POJO对象的支持 >中,已经知道直接对POJO对象进行修改,是不能被绑定的UI组件知道的,在上一篇文章中 ...
- iOS 启动画面 代码自定义
先来看一个可能会遇到的问题: 如果你已经删除了xcode为你的项目自动生成的LaunchScreen.storyboard, 然后你在测试你的app的时候发现,屏幕里出现了黑色的区域,如上图(画红线的 ...
- Websocket简单例子
websocket是Html5的一个协议,也就是说距离我们2016年就几年时间,其他原理我就不说了,直接讲例子 一.准备材料:1.一个开发工具必须支持javaEE7的,原因是javaEE6或以下不支持 ...
- css 选择器优先级
优先级自上而下逐渐递减 1. 在属性后面使用 !important 会覆盖页面内任何位置定义的元素样式. 2.作为style属性写在元素内的样式 3.id选择器 4.类选择器 5.标签选择器 6.通配 ...
- 解决使用osgModeling的Loft生成管子时的bug
最近在使用osgModeling的Loft生成管子的时候, 发现这个类还是有点bug的. 具体的表现就是在某些情况下, 生成管子的某些节点会是扁的, 而且有时管子会莫名的变粗. 在网上各种求助无果 ...
- CSS居中布局总结【转】
居中布局 <div class="parent"> <div class="child">demo</div> </d ...
- Mac 系统环境变量配置
Mac 系统环境变量配置 例如这里要配置一下 QUICK_V3_ROOT 的环境变量 1.打开终端 输入 vim ~/.bash_profile 2.一直回车 知道出现以下选项 按 E 编辑 ...
- iOS开发之Core Animation
在IOS中如果使用普通的动画则可以使用UIKit提供的动画方式来实现,如果想实现更复杂的效果,则需要使用Core Animation了. 在Core Animation中我们经常使用的是 CABasi ...
- Slave作为其它Slave的Master时使用
主从配置需要注意的点 (1)主从服务器操作系统版本和位数一致: (2) Master和Slave数据库的版本要一致: (3) Master和Slave数据库中的数据要一致: (4) Master开启二 ...
- Android课程---关于数据存储的学习(2)
手机外部存储的学习 activity_data2.xml <?xml version="1.0" encoding="utf-8"?> <Li ...