You are implementing a GPU credit ledger that supports adding credits, charging credits, and querying balances. Requests can arrive in any timestamp order (timestamps are not monotonic).
Design a data structure/class that supports these operations:
addCredit(timestamp, amount)
amount
credits were added at time
timestamp
.
chargeCredit(timestamp, amount)
amount
credits were requested to be charged at time
timestamp
.
getBalance(timestamp) -> integer
timestamp
, computed using
all recorded requests whose timestamps are <= timestamp
.
When computing the balance at time T, consider all recorded addCredit and chargeCredit events with timestamp <= T and process them in increasing timestamp order.
0
.
addCredit
, increase the balance.
chargeCredit
:
If multiple events share the same timestamp, process them in this order:
addCredit
events at that timestamp (in insertion order)
chargeCredit
events at that timestamp (in insertion order)
Provide the API and implement the logic so that repeated calls to getBalance(T) always return the correct value according to the rules above.