1. 程式人生 > >optimal-account-balancing

optimal-account-balancing

第一次 code hashmap 租車 ron 試題 之間 pub transfer

一群朋友去度假,有時互相借錢。

例如,愛麗絲為比爾的午餐支付了 10 美元。後來克裏斯給愛麗絲 5 美元搭出租車。我們可以假設每筆交易為一個三元組(X,Y,Z),這意味著第 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 }

optimal-account-balancing