← 返回 instacart 的题目列表Banking System with Accounts, Transfers, Top Activity, and Merge
类型:online_judge
Problem: Implement a Simplified Banking System
Implement a banking system that supports account creation, deposits, payments, top activity queries, delayed transfer acceptance, and account merging.
Every operation has a timestamp in milliseconds. Operations are given in non-decreasing timestamp order.
Operations
Given a list of operations, execute them in order and output the return value of each operation.
1. CREATE timestamp account_id
Create an account.
If account_id does not exist, create it with balance 0 and return true.
If it already exists, return false.
2. DEPOSIT timestamp account_id amount
Deposit money into an account.
If the account does not exist, return None.
Otherwise, increase its balance by amount, add amount to its transaction activity, and return the new balance.
3. PAY timestamp account_id amount
Make a payment from an account.
If the account does not exist or has insufficient balance, return None.
Otherwise, decrease its balance by amount, add amount to its transaction activity, and return the new balance.
4. TOP timestamp n
Return the top n accounts by transaction activity.
Activity is the sum of absolute values of all successful transaction amounts for that account.
Successful DEPOSIT, PAY, and accepted TRANSFER operations count toward activity.
Sort by:
higher activity first;
lexicographically smaller account_id first when tied.
Output format: account_id(activity),account_id(activity).
If fewer than n accounts exist, return all accounts.
5. TRANSFER timestamp source_account target_account amount
Initialize a transfer and return a transfer id.
If either account does not exist, return None.
If both accounts are the same, return None.
If the source account has insufficient balance, return None.
Otherwise, temporarily withdraw amount from the source account, create a unique id such as transfer1, transfer2, and return it.
Transfer initialization itself does not count toward activity.
A transfer must be accepted within 24 * 60 * 60 * 1000 milliseconds.
6. ACCEPT timestamp account_id transfer_id
Accept a transfer.
If transfer_id does not exist, return false.
If the transfer has already been accepted, canceled, or expired, return false.
If account_id is not the transfer target, return false.
If timestamp > created_timestamp + 24 * 60 * 60 * 1000, the transfer expires, the held amount is returned to the source account, and the method returns false.
Otherwise, the transfer succeeds: the target balance increases by amount, both source and target activity increase by amount, and the method returns true.
7. MERGE timestamp account_id_1 account_id_2
Merge account_id_2 into account_id_1.
If either account does not exist, or they are the same account, return false.
Otherwise:
add account_id_2's balance to account_id_1;
add account_id_2's activity to account_id_1;
delete account_id_2;
future TOP results must not include account_id_2.
Pending transfers are handled as follows:
If a pending transfer would become a self-transfer between the merged accounts, cancel it and return its held amount to the merged account.
If the pending transfer source is account_id_2, redirect it to account_id_1.
If the pending transfer target is account_id_2, redirect it to account_id_1.
Input Format
q
operation_1
operation_2
...
operation_q
Output Format
Print one line per operation.
Booleans are printed as true / false.
Missing return values are printed as None.
TOP prints comma-separated account activity entries.
Constraints
1 <= q <= 10^5
1 <= amount <= 10^9
1 <= n <= 10^5
account_id length is at most 32 and contains only letters, digits, and underscores.
Timestamps are non-negative integers and non-decreasing in the input.
Example
Input
10
CREATE 1 alice
CREATE 2 bob
CREATE 3 alice
DEPOSIT 4 alice 100
DEPOSIT 5 bob 50
PAY 6 alice 30
PAY 7 bob 100
TOP 8 2
DEPOSIT 9 carol 20
TOP 10 3
Output
true
true
false
100
50
70
None
alice(130),bob(50)
None
alice(130),bob(50)