482. Max Total Amount in Any 24-Hour Period
Max Total Amount in Any 24-Hour Period

Given unsorted transactions, find the maximum total amount spent in any continuous 24-hour period, either for a given account or across all accounts combined.

Transaction Format

Each transaction is a string "trxId,accountId,timestamp,amount".

  • trxId: unique transaction ID.
  • accountId: account that made the transaction.
  • timestamp: Unix time in seconds.
  • amount: non-negative integer amount. All amounts use the same currency unit.

Method Signatures

long maxSpendForAccount(List<String> transactions, String accountId)
  • Return the maximum total spent by the given account in any 24-hour period.
long maxSpendAllAccounts(List<String> transactions)
  • Return the maximum combined total spent by all accounts within the same 24-hour period.

Return 0 if there are no applicable transactions.

Window Rule

A window starting at start includes timestamps satisfying start ≤ timestamp < start + 86,400. Transactions exactly 24 hours apart cannot be included together. Count separate transactions at the same timestamp individually.

Constraints

  • 0 ≤ transactions.size() ≤ 100,000
  • 0 ≤ timestamp ≤ 1,000,000,000,000
  • 0 ≤ amount ≤ 1,000,000,000
  • Each ID contains between 1 and 20 letters or digits. All transaction IDs are distinct.
  • Every transaction has exactly four comma-separated fields, with no spaces.

Examples

Example 1

transactions = List.of("t4,B2,93600,110", "t1,A1,0,90", "t5,A1,100000,20", "t2,A1,3600,50", "t3,B2,90000,70")

  • maxSpendForAccount(transactions = transactions, accountId = "A1")
    Output: 140. Transactions t1 and t2 total 90 + 50 = 140.
  • maxSpendAllAccounts(transactions = transactions)
    Output: 200. The window [90000, 176400) contains t3, t4, and t5, totaling 70 + 110 + 20 = 200.

Example 2

transactions = List.of("t8,A1,86400,60", "t6,A1,0,100", "t7,B2,0,50")

  • maxSpendForAccount(transactions = transactions, accountId = "A1")
    Output: 100. Transactions t6 and t8 are exactly 24 hours apart, so they cannot be included together.
  • maxSpendAllAccounts(transactions = transactions)
    Output: 150. The window [0, 86400) includes t6 and t7.
  • maxSpendForAccount(transactions = transactions, accountId = "C3")
    Output: 0. Account C3 has no transactions.


Please use Laptop/Desktop or any other large screen to add/edit code.