Leetcode 465. Optimal Account Balancing

Problem:

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:

  1. A transaction will be given as a tuple (x, y, z). Note that x ≠ y and z > 0.
  2. 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.
Solution:

  这道题的要求我们在一堆账户中计算最少的转账次数使得所有账户余额都为0。一开始我还以为要用什么奇淫巧技,后来实在想不出来就尝试了下暴力解法,也就是DFS,结果就做出来了。这里有一个重要的优化的地方,就是每次转账必须要两个账户符号不同(想想也是,只有富人接济穷人,哪有富人接济富人或穷人接济穷人的道理),否则会超时。

Code:

 

 1 class Solution {
 2 public:
 3     void dfs(int index,int times,vector<int> &data,int &result){
 4         if(index == data.size()){
 5             result = min(result,times);
 6             return;
 7         }
 8         if(data[index] == 0)
 9             dfs(index+1,times,data,result);
10         else if(data[index] > 0){
11             for(int i = index+1;i != data.size();++i){
12                 if(data[i] < 0){
13                     data[i] += data[index];
14                     dfs(index+1,times+1,data,result);
15                     data[i] -= data[index];
16                 }
17             }
18         }
19         else{
20             for(int i = index+1;i != data.size();++i){
21                 if(data[i] > 0){
22                     data[i] += data[index];
23                     dfs(index+1,times+1,data,result);
24                     data[i] -= data[index];
25                 }
26             }
27         }
28     }
29     int minTransfers(vectorint>>& transactions) {
30         int m = transactions.size();
31         unordered_map<int,int> um;
32         for(int i = 0;i != m;++i){
33             um[transactions[i][0]] -= transactions[i][2];
34             um[transactions[i][1]] += transactions[i][2];
35         }
36         vector<int> data;
37         for(auto iter:um){
38             if(iter.second != 0)
39                 data.push_back(iter.second);
40         }
41         int result = INT_MAX;
42         dfs(0,0,data,result);
43         return result;
44     }
45 };

 

转载于:https://www.cnblogs.com/haoweizh/p/10233890.html

你可能感兴趣的:(Leetcode 465. Optimal Account Balancing)