NewProject Coding is here — build features and fix bugs in real codebases with an AI assistantSee what's new

A financial company provides customers a way to buy something now and pay for it later by issuing them loans. However, the correct parties need to be paid the correct amounts - merchants need to be paid the full amount upfront, even though customers are paying that amount over time. This means that money cannot be dispensed directly from the customers to the merchants. Therefore, in order for the logistics of buy now pay later to happen, money needs to be moved between multiple parties - customers, companies, merchants, and third-party entities that the company works with.

Given a list of pending transactions and the current end-of-day bank account balances, determine the updated balance for each party after applying the transactions. Return a list sorted alphabetically by party name.

Constraints:

  • Each transaction involves two parties and a positive integer amount.
  • Initial balances and transaction amounts are integers.
  • Ignore the number limit of transactions and merchants.

Example 1:

Input: transactions = [["Alice","Bob","50"],["Bob","Charlie","30"],["Charlie","Alice","20"],["Alice","David","70"]]
initialBalance = [["Alice","100"],["Bob","50"],["Charlie","75"],["David","25"]]
Output: [0, 70, 85, 95]
Explanation: After all transactions are enacted, the end-of-day balances for "Alice", "Bob", "Charlie," and "David" (sorted alphabetically) are calculated as:

  • Alice: 100 (initial) - 50 (to Bob) - 70 (to David) + 20 (from Charlie) = 0
  • Bob: 50 (initial) + 50 (from Alice) - 30 (to Charlie) = 70
  • Charlie: 75 (initial) + 30 (from Bob) - 20 (to Alice) = 85
  • David: 25 (initial) + 70 (from Alice) = 95

Example 2:

Input: transactions = [["Alice","Bob","300"],["Charlie","David","400"],["Eve","Frank","200"],["George","Hank","100"],["Alice","Charlie","150"],["David","Eve","300"],["Frank","George","250"],["Bob","Hank","100"],["Charlie","Alice","200"],["Eve","David","150"]]
initialBalance = [["Alice","1000"],["Bob","600"],["Charlie","500"],["David","700"],["Eve","600"],["Frank","400"],["George","200"],["Hank","100"],["Ivy","300"],["Jack","400"]]
Output: [750, 800, 50, 950, 550, 350, 350, 300, 300, 400]

Example 3:

Input: transactions = [["Alice","Bob","150"],["Charlie","Alice","200"],["David","Bob","50"],["Eve","Charlie","100"],["Bob","Eve","300"],["David","Alice","50"],["Alice","Eve","100"],["Charlie","Bob","150"]]
initialBalance = [["Alice","500"],["Bob","300"],["Charlie","200"],["David","150"],["Eve","250"]]
Output: [500, 350, -50, 50, 550]


Accepted
2k/3.3k
Acceptance Rate59.8%
Test Cases
Input
transactions =
initialBalance =
Output
Expected