Rebalance Bank Accounts to a Minimum Balance
Problem statement
Stripe tracks money across bank accounts and sometimes moves funds so that every account stays at or above a required minimum balance.
Each input row is accountName,balance. Return a working sequence of transfers in the form from,to,amount that leaves every account with at least threshold.
An optimal number of transfers is not required. For deterministic output, process underfunded accounts in input order and take funds from overfunded accounts in input order. Move as much as possible in each transfer without taking a donor below the threshold or raising the current receiver above it.
Function
rebalanceAccounts(accounts: String[], threshold: long) → String[]Examples
Example 1
accounts = ["AU,80","US,140","MX,110","SG,120","FR,70"]threshold = 100return = ["US,AU,20","US,FR,20","MX,FR,10"]US first fills AU, then contributes its remaining surplus to FR. MX supplies FR's final ten.
Example 2
accounts = ["a,50","b,50","c,200"]threshold = 100return = ["c,a,50","c,b,50"]The only donor funds both receivers in their input order.
Constraints
1 <= accounts.length <= 500.- Account names are unique and contain no commas.
0 <= balance, threshold <= 10^12.- The total balance is at least
accounts.length * threshold. - All arithmetic fits in signed 64-bit integers.