1. 程式人生 > >[LeetCode] 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 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.

這道題給了一堆某人欠某人多少錢這樣的賬單,問我們經過優化後最少還剩幾個。其實就相當於一堆人出去玩,某些人可能幫另一些人墊付過花費,最後結算總花費的時候可能你欠著別人的錢,其他人可能也欠你的欠。我們需要找出簡單的方法把所有欠賬都還清就行了。這道題的思路跟之前那道Evaluate Division有些像,都需要對一組資料顛倒順序處理。我們使用一個雜湊表來建立每個人和其賬戶的對映,其中賬戶若為正數,說明其他人欠你錢;如果賬戶為負數,說明你欠別人錢。我們對於每份賬單,前面的人就在雜湊表中減去錢數,後面的人在雜湊表中加上錢數。這樣我們每個人就都有一個賬戶了,然後我們接下來要做的就是合併賬戶,看最少需要多少次匯款。我們先統計出賬戶值不為0的人數,因為如果為0了,表明你既不欠別人錢,別人也不欠你錢,如果不為0,我們把錢數放入一個數組accnt中,然後呼叫遞迴函式。在遞迴函式中,我們初始化結果res為整型最大值,然後我們跳過為0的賬戶,然後我們開始遍歷之後的賬戶,如果當前賬戶和之前賬戶的錢數正負不同的話,我們將前一個賬戶的錢數加到當前賬戶上,這很好理解,比如前一個賬戶錢數是-5,表示張三欠了別人五塊錢,當前賬戶錢數是5,表示某人欠了李四五塊錢,那麼張三給李四五塊,這兩人的賬戶就都清零了。然後我們呼叫遞迴函式,此時從當前改變過的賬戶開始找,num表示當前的轉賬數,需要加1,然後我們用這個遞迴函式返回的結果來更新res,後面別忘了復原當前賬戶的值。遍歷結束後,我們看res的值如果還是整型的最大值,說明沒有改變過,我們返回num,否則返回res即可,參見程式碼如下:

class Solution {
public:
    int minTransfers(vector<vector<int>>& transactions) {
        unordered_map<int, int> m;
        for (auto t : transactions) {
            m[t[0]] -= t[2];
            m[t[1]] += t[2];
        }
        vector<int> accnt(m.size());
        int cnt = 0;
        for (auto a : m) {
            if (a.second != 0) accnt[cnt++] = a.second;
        }
        return helper(accnt, 0, cnt, 0);
    }
    int helper(vector<int>& accnt, int start, int n, int num) {
        int res = INT_MAX;
        while (start < n && accnt[start] == 0) ++start;
        for (int i = start + 1; i < n; ++i) {
            if ((accnt[i] < 0 && accnt[start] > 0) || (accnt[i] > 0 && accnt[start] < 0)) {
                accnt[i] += accnt[start];
                res = min(res, helper(accnt, start + 1, n, num + 1));
                accnt[i] -= accnt[start];
            }
        }
        return res == INT_MAX ? num : res;
    }
};

類似題目:

參考資料: