当前位置:网站首页>465. DFS backtracking of optimal bill balance
465. DFS backtracking of optimal bill balance
2022-07-03 00:59:00 【Empress Yu】
465. Optimal bill balance
A group of friends will borrow money from each other during their holidays . for instance , Xiao AI paid Xiao Xin's lunch for a total of 10 dollar . If Xiao Ming pays Xiao AI's taxi money, the total amount is 5 dollar . We can use a triple (x, y, z) Indicates a transaction , Express x lend y total z dollar . use 0, 1, 2 I love my classmates 、 Xiao Xin and Xiao Ming (0, 1, 2 Human label ), The above transaction can be expressed as
[[0, 1, 10], [2, 0, 5]].Given a list of transaction information between a group of people , Calculate the minimum number of times you can pay off all your debts .
Be careful :
- A trade fair is represented by triples (x, y, z) Express , And there are
x ≠ yAndz > 0.- People's labels may not be in order , For example, the label may be 0, 1, 2 Also may be 0, 2, 6.
Example 1:
Input : [[0,1,10], [2,0,5]] Output : 2 explain : people #0 Give a person #1 total 10 dollar . people #2 Give a person #0 total 5 dollar . Need two transactions . One way is people #1 Give people separately #0 And people #2 various 5 dollar .Example 2:
Input : [[0,1,10], [1,0,1], [1,2,5], [2,0,5]] Output : 1 explain : people #0 Give a person #1 total 10 dollar .Person #0 gave person #1 $10. people #1 Give a person #0 total 1 dollar .Person #1 gave person #0 $1. people #1 Give a person #2 total 5 dollar .Person #1 gave person #2 $5. people #2 Give a person #0 total 5 dollar .Person #2 gave person #0 $5. therefore , people #1 Need to give people #0 total 4 dollar , All debts can be paid off .source : Power button (LeetCode)
link :https://leetcode.cn/problems/optimal-account-balancing
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The result of doing the question
success ( Almost failed . Encountered two wrong ideas , The first mistake was to write greedy and change it twice , Then I realized that greedy error is polynomial ; The second is enumeration, which only enumerates one side , Because the length of positive and negative sets is different , So both sides should be used , Enumeration is required , Learned here , Only those with equal length matching can be enumerated on one side , Otherwise, enumerate on both sides )
Method :DFS
- Calculate the amount of each person (x,y,z) , x many 10 element , y Less 10 element 【 And vice versa , Ensure the same direction of calculation 】
- 0 The balance of yuan is ignored , The rest , Positive numbers are grouped , Negative numbers are grouped
- DFS Enumerate all the situations :
- The balance is 0: Both positive and negative amounts consume one
- Negative balance , Positive numbers consume one
- The balance is positive , Negative numbers consume one
- The balance is 0, And the positive and negative sets have been cycled to the end , end , Compare the smallest answer
class Solution {
public int minTransfers(int[][] transactions) {
Map<Integer,Integer> map = new HashMap<>();
for(int[] transaction:transactions){
int a = transaction[0];
int b = transaction[1];
int c = transaction[2];
map.put(a,map.getOrDefault(a,0)+c);
map.put(b,map.getOrDefault(b,0)-c);
}
List<Integer> a = new ArrayList<>();
List<Integer> b = new ArrayList<>();
for(int money:map.values()){
if(money>0) a.add(money);
if(money<0) b.add(money);
}
dfs(a,b,0,0,0,0);
return ans;
}
int ans = Integer.MAX_VALUE;
private void dfs(List<Integer> a, List<Integer> b, int last, int ia, int ib,int time){
if(ia == a.size() && ib == b.size()){
ans = Math.min(time,ans);
return;
}
if(last>0){
for(int i = ib; i < b.size(); i++){
swap(b,ib,i);
dfs(a,b,last+b.get(ib),ia,ib+1,time+1);
swap(b,ib,i);
}
return;
}
for(int i = ia; i < a.size(); i++){
swap(a,ia,i);
if(last == 0){
dfs(a,b,last+a.get(ia)+b.get(ib),ia+1,ib+1,time+1);
}else{
dfs(a,b,last+a.get(ia),ia+1,ib,time+1);
}
swap(a,ia,i);
}
}
private void swap(List<Integer> a, int x, int y){
int temp = a.get(x);
a.set(x,a.get(y));
a.set(y,temp);
}
}边栏推荐
- [AUTOSAR VI description document]
- 【爱死机】《吉巴罗》被忽略的细节
- [overview of AUTOSAR four BSW]
- 【AutoSAR 二 AppL概述】
- leetcode-871:最低加油次数
- mysql 多表联合删除
- The arm core board / development board of Feiling equipped with Ti am62x made its debut in embedded world 2022
- Machine learning: numpy version linear regression predicts Boston house prices
- Vulkan practice first bullet
- Vulkan is not a "panacea"“
猜你喜欢
![[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)](/img/f5/3ec22f1480227f33a1c8ac457155ed.jpg)
[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)

tail -f 、tail -F、tailf的区别
![[love crash] neglected details of gibaro](/img/d6/baa4b5185ddaf88f3df71a94a87ee2.jpg)
[love crash] neglected details of gibaro
![[AUTOSAR + IO Architecture]](/img/cf/9ea42b50bed298c0546764b63bd957.png)
[AUTOSAR + IO Architecture]

leetcode-849:到最近的人的最大距离

Unity learns from spaceshooter to record the difference between fixedupdate and update in unity for the second time

12_微信小程序之微信视频号滚动自动播放视频效果实现

Rust string slicing, structs, and enumeration classes

【AutoSAR 三 RTE概述】

Illustrated network: what is virtual router redundancy protocol VRRP?
随机推荐
瑞萨RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南
[jetcache] jetcache configuration description and annotation attribute description
University of Oslo: Li Meng | deep reinforcement learning based on swing transformer
Meaning of Tencent cloud free SSL certificate extension file
[AUTOSAR 11 communication related mechanism]
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
Test shift right: Elk practice of online quality monitoring
RK3568开发板评测篇(二):开发环境搭建
Win10 多种方式解决无法安装.Net3.5的问题
【AutoSAR 六 描述文件】
tail -f 、tail -F、tailf的区别
【案例分享】让新时代教育发展与“数”俱进
Shell implements basic file operations (SED edit, awk match)
【AutoSAR 四 BSW概述】
1.12 - 指令
2022.2.14 resumption
【AutoSAR 十二 模式管理】
Vulkan-性能及精细化
【AutoSAR 七 工具链简介】
Leetcode-1964: find the longest effective obstacle race route to each position