← 返回 rippling 的题目列表Driver Balances with Cached Total Balance
类型:online_judge
You need to maintain balances for delivery drivers (driverId is a string or integer) and answer very frequent queries for the total balance across all drivers.
Design a data structure / API supporting:
add(driverId, delta): increase the driver’s balance by delta (delta can be negative). If the driver doesn’t exist, treat initial balance as 0.
get(driverId): return the driver’s current balance; return 0 if missing.
getTotal(): return the sum of balances of all drivers.
Constraints/requirements:
getTotal() is called extremely frequently (possibly inside loops), so it should be as fast as possible.
State target time complexities and implement the core logic.
Scale (for complexity reasoning):
Number of operations Q up to 2e5
Number of distinct drivers N up to 2e5
delta in [-1e9, 1e9]
Example:
add(A,10), add(B,5), getTotal() => 15
add(A,-3), get(A) => 7, getTotal() => 12
get(C) => 0, getTotal() still 12
Example
Input
9
add A 10
add B 5
getTotal
add A -3
get A
getTotal
get C
add C 8
getTotal
Output
15
7
12
0
20