Practice/Netflix/Netflix internal bank (usual DS&A no tricks)
CodingOptional
You're building an internal transaction settlement system for a company. Employees can make transactions with each other throughout a billing period, and at the end of the period, you need to calculate the minimum number of settlement transactions required to clear all debts.
Given a list of transactions where each transaction is represented as [debtor, creditor, amount] (meaning the debtor owes the creditor that amount), calculate the minimal set of transactions needed to settle all debts between users.
[debtor, creditor, amount]Example 1:
` Input: [["alice", "bob", 50], ["bob", "charlie", 30], ["charlie", "alice", 20]] Output: [["alice", "bob", 30]] Explanation:
Example 2:
Input: [["alice", "bob", 100], ["bob", "alice", 100]] Output: [] Explanation: The debts cancel out perfectly, so no settlement is needed.
Example 3:
Input: [["user1", "user2", 30], ["user1", "user2", 20], ["user2", "user1", 15]] Output: [["user1", "user2", 35]] Explanation: Combine all transactions between user1 and user2: 30 + 20 - 15 = 35 net owed by user1