Company: Goldman Sachs_15 july
Difficulty: medium
Transaction Simplification Problem Description Implement a prototype service to simplify a group of debt transactions. There are n people, and a list of m debts amongst them where debt[i] = [fromID, toID, amount] represents that person fromID owes the person toID an amount of amount . Given the array debts , find the minimum number of transactions required to clear all the debts. Example: Suppose n=3 , m=4 , debts = [[0, 1, 10], [1, 0, 20], [2, 0, 5], [0, 2, 10]] from | to | amount -----|----|------- 0 | 1 | 10 1 | 0 | 20 2 | 0 | 5 0 | 2 | 10 Suppose 0 gives 1 a total amount of 5 units. from | to | amount -----|----|------- 0 | 1 | 5 1 | 0 | 10 2 | 0 | 5 0 | 2 | 10 It also owed 0-5 units, so reduce the debt from 1 to 0 by that 5 units. Now 0 and 1's debts are simplified. from | to | amount -----|----|------- 0 | 1 | 10 1 | 0 | 10 2 | 0 | 10 The three transactions can now cancel each other out. Only one transaction is required to clear all the debts, i.e., from 0 to 1. Hence, the answer