optimal-account-balancing

一群朋友去度假,有时互相借钱。

例如,爱丽丝为比尔的午餐支付了 10 美元。后来克里斯给爱丽丝 5 美元搭出租车。我们可以假设每笔交易为一个三元组(X,Y,Z),这意味着第 个人借给第 个人 美元。假设 Alice,Bill 和 Chris 是第0,1,2  个人(0,1,2是他们的ID),他们之间的交易可以表示为[ [ 0,1,10 ],[ 2,0,5 ] ]。

给定一组人之间的交易清单,返回结算所需的最低交易数量

九章算法一篇置顶题目 挺有意思 第一次看到这种需要指数级复杂度的面试题

下面的解法非常具有局限性 如果 debt非零的人多于32 或 64 下面的方法是行不通的

 1 public class Solution {
 2
 3       public int minTransfer(int[][] transactions){
 4            Map<Integer, Integer> debts = new HashMap<Integer, Integer>();
 5            for(int[] t: transactions){
 6                    debts.put(t[0], getOrDefault(t[0], 0)-t[2]);
 7                    debts.put(t[1], getOrDefault(t[1], 0)+t[2]);
 8            }
 9            int len = 0;
10            int[] account =  new int[debts.size()];
11            for(int total: debts.values()){
12                if(total!=0){
13                   account[len++] = total;
14                }
15            }
16            if(len ==0) return 0;
17            int dp = new int[1<<len];
18            Arrays.fill(dp, Integer.MAX_VALUE);
19            for(int l =1; l <dp.length; l++){
20                    int sum =0;
21                    int count =0;
22                    for(int i=0; i<len;i++){
23                           if((1<<i&l)!=0){
24                                 sum + = account[i];
25                                 count ++;
26                           }
27                    }
28                    if(sum ==0){
29                            dp[l] = count-1;
30                            for(int i =1; i<l;i++){
31                                  if((i&l)==i &&(dp[i]+dp[l-i])< dp[l]){
32                                         dp[l] = dp[i]+dp[l-i];
33                                  }
34                            }
35                    }
36            }
37            return dp[len-1];
38       }
39
40 }
时间: 2024-10-10 20:47:57

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 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 per

继续过Hard题目.0209

http://www.cnblogs.com/charlesblc/p/6372971.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca

练练脑,继续过Hard题目

http://www.cnblogs.com/charlesblc/p/6384132.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca

继续过Hard题目

接上一篇:http://www.cnblogs.com/charlesblc/p/6283064.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 L

练几道,继续过Hard题目

http://www.cnblogs.com/charlesblc/p/6384132.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

LIST OF NOSQL DATABASES [currently 150]

http://nosql-database.org Core NoSQL Systems: [Mostly originated out of a Web 2.0 need] Wide Column Store / Column Families Hadoop / HBase API: Java / any writer, Protocol: any write call, Query Method: MapReduce Java / any exec, Replication: HDFS Re

How Network Load Balancing Technology Works--reference

http://technet.microsoft.com/en-us/library/cc756878(v=ws.10).aspx In this section Network Load Balancing Terms and Definitions Network Load Balancing Architecture Network Load Balancing Protocols Application Compatibility with Network Load Balancing

Network Load Balancing Technical Overview--reference

http://technet.microsoft.com/en-us/library/bb742455.aspx Abstract Network Load Balancing, a clustering technology included in the Microsoft Windows 2000 Advanced Server and Datacenter Server operating systems, enhances the scalability and availabilit