Hash map · Easy
A ledger export contains signed integer-cent adjustments. A report needs one balance per account.
Sum adjustments per account and return accounts in ASCII order.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ entries:{account:string,cents:integer}[] }
What each field means
entries— ledger adjustments.accountidentifies the account; signedcentsadds to or subtracts from its balance.
Output
Return value and rules
- Return one
{account, cents}total per account that appeared. - Include accounts whose adjustments sum to zero.
- Sort accounts by ASCII ID ascending.
Constraints
- 0 <= entries.length <= 100000
- Account IDs contain 1–40 lowercase ASCII letters; cents in [-1000000,1000000].
Examples
Example 1
{
"entries": [
{
"account": "b",
"cents": 5
},
{
"account": "a",
"cents": 3
},
{
"account": "b",
"cents": -2
}
]
}Returns
[
{
"account": "a",
"cents": 3
},
{
"account": "b",
"cents": 3
}
]Example 2
{
"entries": []
}Returns
[]Required complexity
O(n + a log a) time and O(a) space