Problem Description
Given an array of transactions in the form "name,time,amount,city", a transaction is possibly invalid if its amount exceeds $1000, or if it occurs within (and including) 60 minutes of another transaction with the same name in a different city. The task is to return all transactions that meet the invalid criteria.
Key Insights
- Parse each transaction string into its components: name, time, amount, and city.
- Immediately mark any transaction as invalid if its amount exceeds 1000.
- Group transactions by the same name using a hash table.
- For each group, compare transactions pairwise to check if any two with the same name occur within 60 minutes while being in different cities.
- Use a set to store the results to avoid duplicates.
Space and Time Complexity
Time Complexity: O(n^2) in the worst-case due to pairwise comparisons within each name group. Space Complexity: O(n) for storing parsed transactions and grouping them by name.
Solution
The solution begins by parsing each transaction into its individual parts. A dictionary (or hash map) is used to group transactions by name, which reduces unnecessary comparisons. For each transaction, if the amount is greater than 1000, mark it as invalid. Then, for each pair of transactions with the same name, if the time difference is within 60 minutes and the cities are different, mark the involved transactions as invalid. This covers both conditions of potential invalidity.