Beta

Minimum Transactions

8 of 10lonkaan

Description:

This Kata is a bit more optimized version of minimum account balancing problem.

You are given a list of transactions between people in the form of ( payer, payee, amount ) .

This means payer pays amount to payee, so that payee owes amount to payer.

Return the list of minimum number of transactions, with minimum amount of total money transferred, so that everyone pays back what they owed. Each transaction should be in the form of ( payer, payee, amount ) .

Example #1

l = [(3, 0, 7), (3, 1, 5), (3, 2, 1), (4, 2, 7)]
min_transaction(l) = [(2, 3, 8), (0, 4, 7), (1, 3, 5)]
l = [(3, 0, 7), (3, 1, 5), (3, 2, 1), (4, 2, 7)]
minTransaction l = [(2, 3, 8), (0, 4, 7), (1, 3, 5)]
l = [[3, 0, 7], [3, 1, 5], [3, 2, 1], [4, 2, 7]]
minTtransaction(l) = [[2, 3, 8], [0, 4, 7], [1, 3, 5]]

Input/Output means

  • Person 0 took 7, so should give 7 in total
  • Person 1 took 5, so should give 5 in total
  • Person 2 took 8, so should give 8 in total
  • Person 3 gave 13, so should get 13 in total
  • Person 4 gave 7, so should get 7 in total

We can balance these transactions with the following 3 transactions:

  • Person 2 pays 8 to person 3
  • Person 1 pays 5 to person 3
  • Person 0 pays 7 to person 4

Example #2

l = [(4, 1, 7), (4, 8, 7), (1, 8, 22), (8, 2, 24), (1, 8, 21), (8, 1, 8)]
Possible solution    -> [(2, 1, 24), (8, 1, 4), (8, 4, 14)]
Alternative solution -> [(2, 4, 14), (2, 1, 10), (8, 1, 18)]
l = [(4, 1, 7), (4, 8, 7), (1, 8, 22), (8, 2, 24), (1, 8, 21), (8, 1, 8)]
Possible solution    -> [(2, 1, 24), (8, 1, 4), (8, 4, 14)]
Alternative solution -> [(2, 4, 14), (2, 1, 10), (8, 1, 18)]
l = [[4, 1, 7], [4, 8, 7], [1, 8, 22], [8, 2, 24], [1, 8, 21], [8, 1, 8]]
Possible solution    -> [[2, 1, 24], [8, 1, 4], [8, 4, 14]]
Alternative solution -> [[2, 4, 14], [2, 1, 10], [8, 1, 18]]

Constraints

  • Number of people will not exceed 8
  • Transaction list can contain up to 100 transactions
  • Transaction amount cannot be negative
  • In the resulting list, some people can owe money to someone that s/he didnt take money directly from. That is fine; we are trying to minimize the number oftransactions and ( then ) the minimum amount of money that should be transferred

If there is no one that should pay anything just return empty list.

There can be multiple solutions. It is enough to return one of them.
Your solution will be tested in terms of correct minimum transaction count, correct minimum transaction amount, and people paying or receiving exactly what they owe or are owed.
Transaction order is not important.

Algorithms
Graph Theory

More By Author:

Check out these other kata created by lonkaan

Stats:

CreatedMay 8, 2020
PublishedMay 8, 2020
Warriors Trained83
Total Skips4
Total Code Submissions266
Total Times Completed10
Python Completions8
JavaScript Completions5
Haskell Completions1
Total Stars11
% of votes with a positive feedback rating90% of 5
Total "Very Satisfied" Votes4
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes0
Total Rank Assessments4
Average Assessed Rank
4 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
4 kyu
Ad
Contributors
  • lonkaan Avatar
  • JohanWiltink Avatar
Ad