Skip to content

Instantly share code, notes, and snippets.

@calebperkins
Created December 4, 2014 01:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calebperkins/c4f810acbf55a1d767e3 to your computer and use it in GitHub Desktop.
Save calebperkins/c4f810acbf55a1d767e3 to your computer and use it in GitHub Desktop.
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
payers = 0
receivers = 0
for amount in wealth.itervalues():
if amount > 0:
receivers += 1
elif amount < 0:
payers += 1
return max(payers, receivers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment