当前位置:网站首页>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);
}
}边栏推荐
- 飞凌搭载TI AM62x的ARM核心板/开发板首发上市,亮相Embedded World 2022
- Teach you JDBC hand in hand -- structure separation
- In the first half of 2022, there are 10 worth seeing, and each sentence can bring you strength!
- Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
- 文件操作IO-Part2
- How to find out the currently running version of Solr- How do I find out version of currently running Solr?
- Foundations of data science is free to download
- 详解RDD基本概念、RDD五大属性
- kubernetes资源对象介绍及常用命令(五)-(NFS&PV&PVC)
- [introduction to AUTOSAR seven tool chain]
猜你喜欢
随机推荐
世平信息首席科学家吕喆:构建以数据和人员为中心的安全能力
Teach you JDBC hand in hand -- structure separation
【AutoSAR 一 概述】
465. 最优账单平衡 DFS 回溯
【AutoSAR 八 OS】
KingbaseES ALTER TABLE 中 USING 子句的用法
Cordova plugin device obtains the device information plug-in, which causes Huawei to fail the audit
Win10 多种方式解决无法安装.Net3.5的问题
Win10 can't be installed in many ways Problems with NET3.5
图解网络:什么是虚拟路由器冗余协议 VRRP?
Arduino开发之按键检测与正弦信号输出
leetcode-2280:表示一个折线图的最少线段数
腾讯云免费SSL证书扩展文件含义
Overlay of shutter (Pop-Up)
[AUTOSAR 11 communication related mechanism]
基于ARM RK3568的红外热成像体温检测系统
FPGA - 7系列 FPGA内部结构之Clocking -04- 多区域时钟
How to convert Quanzhi a40i/t3 to can through SPI
Attributeerror: 'tuple' object has no attribute 'layer' problem solving
Tensorflow 2.x(keras)源码详解之第十五章:迁移学习与微调


![[overview of AUTOSAR three RTE]](/img/6a/0df33beb42f165af77a17b5d8b01e2.png)






