GPU Credit Ledger With Expiry
MediumSimulation ยท OpenAI
Process a ledger of compute-credit events in order and return one result per spend and balance event, in order.
["grant", t, amount, expires_at]addsamountcredits, usable at timest'witht <= t' < expires_at.["spend", t, amount]takesamountcredits from the unexpired grants, the grant with the earliestexpires_atfirst. If the unexpired credits total less thanamount, nothing is taken and the result is"insufficient"; otherwise"ok".["balance", t]returns the unexpired credits remaining, as an integer.
Event times never decrease.
Example 1
- Input:
- events = [["grant", 0, 50, 10], ["grant", 2, 30, 5], ["balance", 3], ["spend", 4, 40], ["balance", 5]]
- Output:
- [80, "ok", 40]
- Explanation
- The spend takes all 30 credits expiring at 5, then 10 of the other grant, leaving 40.
Example 2
- Input:
- events = [["grant", 1, 20, 6], ["spend", 2, 25], ["balance", 2], ["spend", 6, 1]]
- Output:
- ["insufficient", 20, "insufficient"]
- Explanation
- The first spend takes nothing. At time 6 the grant has expired.
Example 3
- Input:
- events = [["balance", 0]]
- Output:
- [0]
Constraints
0 <= events.length <= 20000 <= t <= 10^9, non-decreasing acrossevents1 <= amount <= 10^9in every grant and spend- A grant may have
expires_at <= t, in which case it is already expired