GPU Credit Ledger with a Negative-Balance Failure State
Problem statement
Process a time-aware GPU credit ledger from the operations in operations.
Supported operations
ADD grantId amount start expire: add a grant with positive credits. It is active whenstart <= timestamp < expire.SUBTRACT amount timestamp: spend active credits, consuming grants with the earliest expiration first. Grants with equal expiration are consumed in insertion order.BALANCE timestamp: append the active remaining balance as a decimal string.
Operation timestamps are nondecreasing. If a subtraction exceeds the active balance, the ledger enters a permanent negative-balance failure state. That subtraction does not need a returned value, and every later BALANCE appends NONE. Later additions or subtractions cannot recover the failed ledger.
Function
processGpuCreditsUntilNegative(operations: String[]) → String[]Examples
Example 1
operations = ["ADD a 4 0 10","ADD b 3 0 5","SUBTRACT 5 1","BALANCE 1","BALANCE 6"]return = ["2","2"]The subtraction consumes all three credits from b and two from a. The two remaining credits in a are active at both queried timestamps.
Example 2
operations = ["ADD a 2 0 10","SUBTRACT 3 1","BALANCE 1","ADD b 5 2 20","BALANCE 2"]return = ["NONE","NONE"]The subtraction exceeds the two active credits and permanently fails the ledger, so both later balance queries return NONE.
Constraints
1 <= operations.length <= 100000.- Grant identifiers are nonempty and unique.
1 <= amount <= 10^9and0 <= start < expire <= 10^9.- Operation timestamps are nondecreasing.
- All valid balances fit in a signed 64-bit integer.