Leetcode: Optimal Account Balancing

介绍了一种算法,用于计算一组人之间互相借贷后,达到债务平衡所需的最少交易次数。通过回溯和贪婪策略来减少交易步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 1 public class Solution {
 2     int res = Integer.MAX_VALUE;
 3     public int minTransfers(int[][] transactions) {
 4         HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
 5         for (int[] transaction : transactions) {
 6             map.put(transaction[0], map.getOrDefault(transaction[0], 0)-transaction[2]);
 7             map.put(transaction[1], map.getOrDefault(transaction[1], 0)+transaction[2]);
 8         }
 9         ArrayList<Integer> depts = new ArrayList<Integer>();
10         for (int dept : map.values()) {
11             if (dept != 0) depts.add(dept);
12         }
13         helper(depts, 0, 0);
14         return res;
15     }
16     
17     public void helper(ArrayList<Integer> depts, int start, int count) {
18         while (start<depts.size() && depts.get(start)==0) start++;
19         if (start == depts.size()) {
20             res = Math.min(res, count);
21             return;
22         }
23         for (int i=start+1; i<depts.size(); i++) {
24             if (depts.get(start)<0&&depts.get(i)>0 || depts.get(start)>0&&depts.get(i)<0) {
25                 depts.set(i, depts.get(i)+depts.get(start));
26                 //int store = depts.get(start);
27                 //depts.set(start, 0);
28                 helper(depts, start+1, count+1);
29                 //depts.set(start, store);
30                 depts.set(i, depts.get(i)-depts.get(start));
31             }
32         }
33     }
34 }

 

内容概要:本文针对国内加密货币市场预测研究较少的现状,采用BP神经网络构建了CCi30指数预测模型。研究选取2018年3月1日至2019年3月26日共391天的数据作为样本,通过“试凑法”确定最优隐结点数目,建立三层BP神经网络模型对CCi30指数收盘价进行预测。论文详细介绍了数据预处理、模型构建、训练及评估过程,包括数据归一化、特征工程、模型架构设计(如输入层、隐藏层、输出层)、模型编译与训练、模型评估(如RMSE、MAE计算)以及结果可视化。研究表明,该模型在短期内能较准确地预测指数变化趋势。此外,文章还讨论了隐层节点数的优化方法及其对预测性能的影响,并提出了若干改进建议,如引入更多技术指标、优化模型架构、尝试其他时序模型等。 适合人群:对加密货币市场预测感兴趣的研究人员、投资者及具备一定编程基础的数据分析师。 使用场景及目标:①为加密货币市场投资者提供一种新的预测工具和方法;②帮助研究人员理解BP神经网络在时间序列预测中的应用;③为后续研究提供改进方向,如数据增强、模型优化、特征工程等。 其他说明:尽管该模型在短期内表现出良好的预测性能,但仍存在一定局限性,如样本量较小、未考虑外部因素影响等。因此,在实际应用中需谨慎对待模型预测结果,并结合其他分析工具共同决策。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值