-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransitiveDebtTransfer.js
More file actions
58 lines (49 loc) · 1.54 KB
/
Copy pathTransitiveDebtTransfer.js
File metadata and controls
58 lines (49 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function findTransitiveWeightSequence(participantsWeightMap){
const transactions = [];
const orderedByWeight = Object.entries(participantsWeightMap).sort((a, b) => a[1] - b[1]);
const participants = orderedByWeight.map(entry => entry[0]);
const weights = orderedByWeight.map(entry => entry[1]);
const sumOfAllWeights = weights.reduce((acc, weight) => acc + weight, 0);
if (sumOfAllWeights > 0.1){ // 0.1 because fuckin js does not know how to subtract/sum
throw new Error("Sum of all weights must be 0");
}
let leftPointer = 0;
let rightPointer = weights.length - 1;
while(true){
if (leftPointer === rightPointer){
break;
}
const leftParticipant = participants[leftPointer];
const rightParticipant = participants[rightPointer];
const leftAmount = weights[leftPointer]; // must be negative
const rightAmount = weights[rightPointer]; // must be positive
if(leftAmount === 0){
break;
}
if (rightAmount === 0){
break;
}
const remaining = leftAmount + rightAmount;
if(remaining >= 0){
transactions.push({
from: leftParticipant,
to: rightParticipant,
amount: -leftAmount
});
weights[leftPointer] = 0;
weights[rightPointer] = remaining;
leftPointer++;
}
else {
transactions.push({
from: leftParticipant,
to: rightParticipant,
amount: rightAmount
});
weights[rightPointer] = 0;
weights[leftPointer] = remaining;
rightPointer--;
}
}
return transactions;
}