465.Optimal Account Balancing

Tags: [memo], [DP], [top_to_bottom_dp], [recursion], [DFS], [backtracking]

Com: {g}

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 ≠ yand 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: DP, Top To Bottom DP, DFS, Backtracking

class Solution(object):
    def minTransfers(self, transactions):
        """
        :type transactions: List[List[int]]
        :rtype: int
        """
        if not transactions:
            return 0

        balances = collections.defaultdict(int)
        for p1, p2, v in transactions:
            balances[p1] -= v
            balances[p2] += v

        rich = []
        poor = []
        for b in balances:
            if balances[b] > 0:
                rich.append(balances[b])
            elif balances[b] < 0:
                poor.append(-balances[b])

        memo = {}
        return self.helper(rich, poor, memo)

    def helper(self, rich, poor, memo):
        r_index = 0
        while r_index < len(rich) and rich[r_index] == 0:
            r_index += 1

        state = (tuple(rich), tuple(poor))
        if state in memo:
            return memo[state]

        # base case
        if r_index == len(rich):
            return 0

        min_num_of_t = sys.maxint

        r_val = rich[r_index]
        for p_index in xrange(len(poor)):
            p_val = poor[p_index]
            if p_val == 0:
                continue

            rich[r_index] -= min(r_val, p_val)
            poor[p_index] -= min(r_val, p_val)

            min_num_of_t = min(min_num_of_t, self.helper(rich, poor, memo))

            poor[p_index] += min(r_val, p_val)
            rich[r_index] += min(r_val, p_val)

        memo[state] = 1 + min_num_of_t
        return 1 + min_num_of_t

Revelation:

  • Each time, we try to let rich give some money to poor(whose current val != 0), until all rich == 0.
  • Since the tuple is immutable, so we don't need to convert the rich and poor into string, we can just tuple as the key of memo.
  • 这道题首先我们想到可以用一个全局变量,recursion不必return val,但需要pass进一个t_counter来累积transfers的数量,但如果这样的话我们就无法用memo了,所以我们不用全局变量,让recursion return val,但与此同时也要改变recursion function的意思,将它的meaning变为:给rich和poor,返回最少需要多少次transfers, 这样我们要remove掉t_counter,这样我们就可以memo (tuple(rich), (poor)), 因为此时recursion的状态可以用rich和poor来描述了.

Note:

  • Time complexity = O(num of states), 很难看出time complexity, 但我们可以猜time complexity是O(n!).

Time Limited Exceeded

class Solution(object):

    def __init__(self):
        self.result = sys.maxint

    def minTransfers(self, transactions):
        """
        :type transactions: List[List[int]]
        :rtype: int
        """
        if not transactions:
            return 0

        balances = collections.defaultdict(int)
        for p1, p2, v in transactions:
            balances[p1] -= v
            balances[p2] += v

        rich = []
        poor = []
        for b in balances:
            if balances[b] > 0:
                rich.append(balances[b])
            elif balances[b] < 0:
                poor.append(-balances[b])

        self.helper(rich, poor, 0)
        return self.result

    def helper(self, rich, poor, t_counter):
        r_index = 0
        while r_index < len(rich) and rich[r_index] == 0:
            r_index += 1

        # base case
        if r_index == len(rich):
            self.result = min(self.result, t_counter)
            return

        r_val = rich[r_index]
        for p_index in xrange(len(poor)):
            p_val = poor[p_index]
            if p_val == 0:
                continue

            rich[r_index] -= min(r_val, p_val)
            poor[p_index] -= min(r_val, p_val)
            self.helper(rich, poor, t_counter + 1)
            poor[p_index] += min(r_val, p_val)
            rich[r_index] += min(r_val, p_val)

results matching ""

    No results matching ""