← 返回 openai 的题目列表GPU Credits Grants: create_grant / subtract / get_balance with expiry and FIFO-by-expire deduction
类型:online_judge
Problem: GPU Credit Grants
Implement a GPU credits ledger that supports creating grants, subtracting credits, and querying balance. Credits are issued as grants, each with an expiration time. When subtracting credits, you must deduct from grants in order of earliest expiration first.
Implement the following operations (or equivalent methods):
create_grant(grant_id, amount, expire_time): create a grant with amount credits that expires after expire_time (expired credits must not be used for future subtraction and must not be counted in balance queries).
subtract(amount, time): perform a persistent deduction of amount credits at timestamp time. The deduction must consume remaining credits from non-expired grants, ordered by increasing expire_time.
get_balance(time): return the available balance at timestamp time, excluding any remaining credits in grants that are expired at time.
Deduction rules
Only non-expired grants can be used.
Deduct in increasing expire_time order.
subtract mutates remaining credits and affects future subtract and get_balance calls.
Constraints
Up to 2 * 10^5 operations
amount is a non-negative integer
expire_time and time are integer timestamps
Example test cases
Deduct earliest-expiring first
create_grant(A, 10, 5)
create_grant(B, 10, 7)
subtract(12, 4)
get_balance(4) => 8
Expired credits not counted
create_grant(A, 10, 5)
get_balance(6) => 0
Subtraction affects the future
create_grant(A, 10, 100)
subtract(3, 1)
get_balance(2) => 7
Skip expired grants when subtracting
create_grant(A, 10, 5)
create_grant(B, 10, 100)
subtract(6, 10)
get_balance(10) => 4
Insufficient balance behavior (needs clarification)
create_grant(A, 5, 100)
subtract(10, 1)
get_balance(1) => ? (confirm expected behavior: clamp to 0 / error / return failure)
Example
Input
create_grant A 10 5
create_grant B 10 7
subtract 12 4
get_balance 4
Output
8