Leetcode:optimal account Balancing

Source: Internet
Author: User


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:

A transaction will be given as a tuple (x, y, z). Note that x ≠ y and z > 0.
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.
Show Company Tags


Backtracking:time complexity O (n!)



Use HASHMAP to store the initial debts of all, negative means the person sends money to others, positive means the Person gets the money from others.



Now if the map was 0, which means the person was all set and free of debts.



Only consider those people with debts (either positive or negative)



Store them in a array, use backtracking and greedy to clear each person's debts from 1st person till last one.



How to clear one person ' s debt? Find a person 2 in the following array that have opposite sign (+->-or-+), and clear Person1 ' s whole debt with Person2 only.



Here's the trick:example: [7,-6,-1], one obvious optimal solution is Person1 pay $6 to Person2, and pay $ to person3. Notice that this optimal solution are equivalent to another solution:



Person1 pay $7 to Person2, and Person2 pay $ to Person3. If doing DFS, everytime we only consider clearing Person1 's debt wholly with another 1 person, we don ' t need to consi Der clearing with and more people, cause clearing with 1 person are already guaranteed to be optimal.






This problem still have some debates in discussion, would check later


 
 
 1 public class Solution {
 2     int res = Integer.MAX_VALUE;
 3     public int minTransfers(int[][] transactions) {
 4         HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
 5         for (int[] transaction : transactions) {
 6             map.put(transaction[0], map.getOrDefault(transaction[0], 0)-transaction[2]);
 7             map.put(transaction[1], map.getOrDefault(transaction[1], 0)+transaction[2]);
 8         }
 9         ArrayList<Integer> depts = new ArrayList<Integer>();
10         for (int dept : map.values()) {
11             if (dept != 0) depts.add(dept);
12         }
13         helper(depts, 0, 0);
14         return res;
15     }
16     
17     public void helper(ArrayList<Integer> depts, int start, int count) {
18         while (start<depts.size() && depts.get(start)==0) start++;
19         if (start == depts.size()) {
20             res = Math.min(res, count);
21             return;
22         }
23         for (int i=start+1; i<depts.size(); i++) {
24             if (depts.get(start)<0&&depts.get(i)>0 || depts.get(start)>0&&depts.get(i)<0) {
25                 depts.set(i, depts.get(i)+depts.get(start));
26                 //int store = depts.get(start);
27                 //depts.set(start, 0);
28                 helper(depts, start+1, count+1);
29                 //depts.set(start, store);
30                 depts.set(i, depts.get(i)-depts.get(start));
31             }
32         }
33     }
34 }





Leetcode:optimal account Balancing


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.