项目作者: ahtrahdis7

项目描述 :
Minimize Cash Flow among a given set of friends who have borrowed money from each other using JavaScript.
高级语言: JavaScript
项目地址: git://github.com/ahtrahdis7/node-splitwise-js.git
创建时间: 2021-07-01T12:28:04Z
项目社区:https://github.com/ahtrahdis7/node-splitwise-js

开源协议:

下载


Splitwise: Minimize Cash Flow Algorithm

Minimize Cash Flow among a given set of friends who have borrowed money from each other.

Given a number of friends who have to give or take some amount of money from one another. Design an algorithm by which the total cash flow among all the friends is minimized.

Approach :

Greedy.
Settle the debts of the people with Max and Min. Credits.

Data Structure Used :

  1. Array : To storr the final outputs.
  2. Map : To store and get remaining individual transaction amounts in O(1) time complexity.

Implementation :

  1. npm install splitwise-js-map
  1. const Splitwise = require('splitwise-js-map');
  2.  
  3. var input = [
  4. {
  5. 'paidBy': 'A',
  6. 'paidFor': { 'B': 300, 'C': 40, 'D': 30 }
  7. },
  8. {
  9. 'paidBy': 'B',
  10. 'paidFor': { 'A': 50, 'B': 100, 'C': 200 }
  11. }
  12. ]
  13.  
  14. const splits = Splitwise(input);
  15. console.log(splits);
Output: [ `from`, `to` , `value`]
  1. [ [ 'C', 'A', 240 ], [ 'B', 'A', 50 ], [ 'D', 'A', 30 ] ]

Updates :

Feel free to submit PRs for any improvements that you find.

!! HAPPY CODING !!