465. 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.

一刷
题解:
提供一系列的transaction, 问还需要几次transaction能平衡balance

最简单的方法:backtracking
从pos开始,如果有debts[i]与debts[pos]符号不同,那么进行交易。

class Solution {
    // DFS
    public int minTransfers(int[][] transactions) {
        Map map = new HashMap<>();
        for(int[] t : transactions){
            long val1 = map.getOrDefault(t[0], 0L);//balance
            long val2 = map.getOrDefault(t[1], 0L);
            map.put(t[0], val1-t[2]);
            map.put(t[1], val2+t[2]);
        }
        
        List list = new ArrayList<>();
        for(long val : map.values()){
            if(val!=0) list.add(val);
        }
        Long[] debts = new Long[list.size()];
        debts = list.toArray(debts);
        return helper(debts, 0, 0);
    }
    
    int helper(Long[] debts, int pos, int count ){
        while(pos=debts.length) return count;
        int res = Integer.MAX_VALUE;
        for(int i=pos+1; i

Speed up:
有一点greedy + backtracking的感觉,首先用sort找出-5, 5这种pair, 然后从debts list中移除。

然后再用如上的backtracking方法

class Solution {
    // DFS
    public int minTransfers(int[][] transactions) {
        Map map = new HashMap<>();
        for(int[] t : transactions){
            long val1 = map.getOrDefault(t[0], 0L);//balance
            long val2 = map.getOrDefault(t[1], 0L);
            map.put(t[0], val1-t[2]);
            map.put(t[1], val2+t[2]);
        }
        
        List list = new ArrayList<>();
        for(long val : map.values()){
            if(val!=0) list.add(val);
        }
        int matchCount = removeMatch(list);
        return matchCount + minTransStartFrom(list, 0);
    }
    
    private int removeMatch(List list) {
        Collections.sort(list);
        int left = 0;
        int right = list.size() - 1;
        int matchCount = 0;
        while (left < right) {
            if (list.get(left) + list.get(right) == 0) {
                list.remove(left);
                list.remove(right - 1);
                right -= 2;
                matchCount++;
            } else if (list.get(left) + list.get(right) < 0) {
                left++;
            } else {
                right--;
            }
        }
        return matchCount;
    }
    
     private int minTransStartFrom(List list, int start) {
        int result = Integer.MAX_VALUE;
        int n = list.size();
        while (start < n && list.get(start) == 0) {
            start++;
        }
        if (start == n) {
            return 0;
        }
        for (int i = start + 1; i < n; i++) {
            if (list.get(i) * list.get(start) < 0) {
                list.set(i, list.get(i) + list.get(start));
                result = Math.min(result, 1 + minTransStartFrom(list, start + 1));
                list.set(i, list.get(i) - list.get(start));
            }
        }
        return result;
    }
}

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