Driver Balance Ledger
Problem statement
Maintain an in-memory balance ledger for delivery drivers. Process each operation in order:
["ADJUST", driverId, delta]: add the signed integerdeltato the driver's current balance. A driver has balance0before the first adjustment.["BALANCE", driverId]: append the driver's current balance to the result. An unseen driver has balance0.["TOTAL"]: append the sum of every driver's current balance to the result.
Only BALANCE and TOTAL operations produce output. Return those values in operation order.
The total-balance operation may be called very frequently, so it should not scan every driver on each call.
Function
processDriverBalances(operations: String[][]) → long[]Examples
Example 1
operations = [["ADJUST","alice","1200"],["ADJUST","bob","800"],["TOTAL"],["BALANCE","alice"],["ADJUST","alice","-300"],["TOTAL"]]return = [2000,1200,1700]After the first two adjustments, the total is 1200 + 800 = 2000. Alice's balance is 1200. Her final adjustment lowers both her balance and the cached total by 300, so the last result is 1700.
Example 2
operations = [["BALANCE","d7"],["TOTAL"],["ADJUST","d7","-50"],["BALANCE","d7"],["TOTAL"]]return = [0,0,-50,-50]An unseen driver starts at 0, and an empty ledger also totals 0. Signed adjustments may make a balance and the overall total negative.
Example 3
operations = [["ADJUST","a","5"],["ADJUST","a","-5"],["ADJUST","b","9"],["TOTAL"],["BALANCE","a"],["BALANCE","b"]]return = [9,0,9]Repeated adjustments accumulate for the same driver. Driver a returns to 0, while driver b contributes the entire total of 9.
Constraints
1 <= operations.length <= 2 * 10^5.- Each operation has one of the documented forms.
- Each
driverIdcontains1to40ASCII letters, digits, hyphens, or underscores. - Each
deltais a decimal integer string between-10^9and10^9, inclusive. - Every individual driver balance and the total balance fit in a signed
64-bit integer.