Created
December 4, 2014 01:46
-
-
Save calebperkins/c4f810acbf55a1d767e3 to your computer and use it in GitHub Desktop.
Minimum transactions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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