LeetCode 1169. Invalid Transactions
原题链接在这里:https://leetcode.com/problems/invalid-transactions/
题目:
A transaction is possibly invalid if:
- the amount exceeds $1000, or;
- if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
Each transaction string transactions[i]
consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction.
Given a list of transactions
, return a list of transactions that are possibly invalid. You may return the answer in any order.
Example 1:
Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"] Output: ["alice,20,800,mtv","alice,50,100,beijing"] Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.
Example 2:
Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"] Output: ["alice,50,1200,mtv"]
Example 3:
Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"] Output: ["bob,50,1200,mtv"]
Constraints:
transactions.length <= 1000
- Each
transactions[i]
takes the form"{name},{time},{amount},{city}"
- Each
{name}
and{city}
consist of lowercase English letters, and have lengths between1
and10
. - Each
{time}
consist of digits, and represent an integer between0
and1000
. - Each
{amount}
consist of digits, and represent an integer between0
and2000
.
题解:
The input tansaction is not sorted based on timestamp.
Could sort it first.
Have a Map with key as name, the value is a list of transactions.
For given transaction, check existing transactions of under this name, poll the ones with time 60 mins before.
For the rest, if any is in different cities. Put it in res set.
Time Complexity: O(n ^ 2). n = transactions.length.
Space: O(n * m). m = average length of transactions.
AC Java:
class Solution { public List<String> invalidTransactions(String[] transactions) { if(transactions == null || transactions.length == 0){ return new ArrayList<String>(); } Arrays.sort(transactions, (a, b) -> { String [] aArr = a.split(","); String [] bArr = b.split(","); return Integer.valueOf(aArr[1]) - Integer.valueOf(bArr[1]); }); HashSet<String> res = new HashSet<>(); HashMap<String, LinkedList<Node>> hm = new HashMap<>(); for(String t : transactions){ String [] parts = t.split(","); if(Integer.valueOf(parts[2]) > 1000){ res.add(t); } int time = Integer.valueOf(parts[1]); String city = parts[3]; hm.putIfAbsent(parts[0], new LinkedList<Node>()); LinkedList<Node> item = hm.get(parts[0]); while(!item.isEmpty() && time - item.peekFirst().time > 60){ item.pollFirst(); } boolean issueFound = false; for(Node n : item){ if(!n.city.equals(city)){ res.add(parts[0] + "," + n.time + "," + n.amount + "," + n.city); res.add(t); } } if(!issueFound){ item.add(new Node(time, city, parts[2])); } } return new ArrayList<String>(res); } } class Node { int time; String city; String amount; public Node(int time, String city, String amount){ this.time = time; this.city = city; this.amount = amount; } }