---
# System prepended metadata

title: 1169. Invalid Transactions
tags: [Leetcode, Bloomberg, Medium]

---

# 1169. Invalid Transactions
###### tags: `Leetcode` `Medium` `Bloomberg`
Link: https://leetcode.com/problems/invalid-transactions/
## 思路
要考虑的edge case有点多
因为一个transaction，可能因为自己amount>1000被删除，也有可能因为和别的transaction间隔时间太多被删除，为了防止重复删除，应该使用set存要删掉的idx
因为要和前面的所有transaction作比较，所以还要存下同一个人之前所有的transaction，这里要注意的是transactions不是按照时间顺序排的，如果按照时间顺序排，放下有一个transaction和当前的已经间隔超过60min，就可以删掉
## Code
```java=
class Solution {
    class Transaction{
        int idx;
        String name;
        int time;
        int amount;
        String city;
        public Transaction(String transaction,int idx){
            String[] splited = transaction.split(",");
            name = splited[0];
            time = Integer.parseInt(splited[1]);
            amount = Integer.parseInt(splited[2]);
            city = splited[3];
            this.idx = idx;
        }
    }
    public List<String> invalidTransactions(String[] transactions) {
        Set<Integer> deletedTransaction = new HashSet<>();
        Map<String, Set<Transaction>> recentTime = new HashMap<>();
        List<String> ans = new ArrayList<>();
        
        for(int i = 0;i < transactions.length;i++){
            Transaction transaction = new Transaction(transactions[i],i);
            
            if(transaction.amount>1000){
                deletedTransaction.add(i);
            }
            
            if(recentTime.containsKey(transaction.name)){
                for(Transaction a:recentTime.get(transaction.name)){
                    if(Math.abs(transaction.time-a.time)<=60 && !transaction.city.equals(a.city)){
                        deletedTransaction.add(a.idx);
                        deletedTransaction.add(i);
                    }
                }
            }
            
            if(!recentTime.containsKey(transaction.name)){
                recentTime.put(transaction.name, new HashSet<Transaction>());
            }
            recentTime.get(transaction.name).add(transaction);
        }
        
        for(int i:deletedTransaction){
            ans.add(transactions[i]);
        }
        return ans;
    }
}
```