Skip to content

Instantly share code, notes, and snippets.

@calebperkins
calebperkins / gist:c4f810acbf55a1d767e3
Created December 4, 2014 01:46
Minimum transactions
import collections
# input: list of transactions in form (a, b, amount) corresponding to "a owes b amount"
def min_transactions(transactions):
# build map of users to change in wealth
wealth = collections.defaultdict(int)
for lendee, lender, amount in transactions:
wealth[lendee] -= amount
wealth[lender] += amount