← 返回 capitalone 的题目列表Banking System with Top Activity Accounts
类型:online_judge
Problem: Implement a Banking System and Query Top Active Accounts
Implement a simple banking system that supports account creation, deposits, transfers, and querying the top N accounts with the highest financial activity.
Operations
Given a list of operations, execute them in order and output the result of each operation.
Supported operations:
1. CREATE_ACCOUNT account_id
Create an account.
If the account already exists, return false.
Otherwise create it with initial balance 0 and activity 0, and return true.
2. DEPOSIT account_id amount
Deposit money into an account.
If the account does not exist, return -1.
Otherwise increase its balance by amount, increase its activity by amount, and return the new balance.
3. TRANSFER from_id to_id amount
Transfer money from from_id to to_id.
If either account does not exist, return -1.
If from_id == to_id, return -1.
If the source account does not have enough balance, return -1.
Otherwise:
decrease from_id balance by amount and increase its activity by amount;
increase to_id balance by amount and increase its activity by amount;
return the source account's balance after the transfer.
4. TOP_ACTIVITY n
Return the top n accounts with the highest activity.
Financial activity indicator is the absolute sum of all successful transaction amounts for an account, including deposits and successful transfers.
Failed transactions are not included.
Sorting rules:
activity descending;
if tied, account_id lexicographically ascending.
Return an array of strings in this format:
["account1(5400)", "account3(4000)", "account2(3000)"]
If fewer than n accounts exist, return all accounts.
Input Format
q
operation_1
operation_2
...
operation_q
Each operation is one of:
CREATE_ACCOUNT account_id
DEPOSIT account_id amount
TRANSFER from_id to_id amount
TOP_ACTIVITY n
Output Format
Print one line per operation.
Boolean values should be printed as true / false.
Arrays should be printed in JSON-like string array format.
Constraints
1 <= q <= 2 * 10^5
1 <= len(account_id) <= 30
1 <= amount <= 10^9
1 <= n <= 10^5
All balances and activity values fit in signed 64-bit integers.
Example
Input
15
CREATE_ACCOUNT account1
CREATE_ACCOUNT account1
CREATE_ACCOUNT account2
DEPOSIT non-existing 2700
DEPOSIT account1 2700
TRANSFER account1 account2 2701
TRANSFER account1 account2 200
TRANSFER account1 account2 2500
DEPOSIT account2 300
CREATE_ACCOUNT account3
DEPOSIT account3 4000
TOP_ACTIVITY 3
DEPOSIT account2 1000
TOP_ACTIVITY 2
TOP_ACTIVITY 5
Output
true
false
true
-1
2700
-1
2500
0
3000
true
4000
["account1(5400)", "account3(4000)", "account2(3000)"]
4000
["account1(5400)", "account2(4000)"]
["account1(5400)", "account2(4000)", "account3(4000)"]