|
1 | 1 | package g1101_1200.s1169_invalid_transactions;
|
2 | 2 |
|
3 |
| -// #Medium #Array #String #Hash_Table #Sorting |
4 |
| -// #2022_03_05_Time_11_ms_(89.89%)_Space_43.2_MB_(79.45%) |
| 3 | +// #Medium #Array #String #Hash_Table #Sorting #2023_06_02_Time_9_ms_(98.31%)_Space_44.9_MB_(7.40%) |
5 | 4 |
|
6 | 5 | import java.util.ArrayList;
|
7 | 6 | import java.util.HashMap;
|
8 | 7 | import java.util.List;
|
9 | 8 | import java.util.Map;
|
10 |
| -import java.util.TreeMap; |
11 | 9 |
|
12 | 10 | public class Solution {
|
13 |
| - public List<String> invalidTransactions(String[] transactions) { |
14 |
| - Map<String, TreeMap<Integer, Transaction>> map = new HashMap<>(); |
15 |
| - List<String> result = new ArrayList<>(); |
16 |
| - for (String transaction : transactions) { |
17 |
| - String[] split = transaction.split(","); |
18 |
| - String name = split[0]; |
19 |
| - int time = Integer.parseInt(split[1]); |
20 |
| - String city = split[3]; |
21 |
| - map.putIfAbsent(name, new TreeMap<>()); |
22 |
| - map.get(name).put(time, new Transaction(time, city)); |
| 11 | + private static class Transaction { |
| 12 | + String name; |
| 13 | + int time; |
| 14 | + int amount; |
| 15 | + String city; |
| 16 | + |
| 17 | + Transaction(String trans) { |
| 18 | + String[] s = trans.split(","); |
| 19 | + name = s[0]; |
| 20 | + time = Integer.parseInt(s[1]); |
| 21 | + amount = Integer.parseInt(s[2]); |
| 22 | + city = s[3]; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + public List<String> invalidTransactions(String[] input) { |
| 27 | + List<String> res = new ArrayList<>(); |
| 28 | + if (input == null || input.length == 0) { |
| 29 | + return res; |
23 | 30 | }
|
24 |
| - for (String transaction : transactions) { |
25 |
| - String[] split = transaction.split(","); |
26 |
| - String name = split[0]; |
27 |
| - int time = Integer.parseInt(split[1]); |
28 |
| - int amount = Integer.parseInt(split[2]); |
29 |
| - String city = split[3]; |
30 |
| - if (amount > 1000) { |
31 |
| - result.add(transaction); |
32 |
| - continue; |
| 31 | + Map<String, List<Transaction>> map = new HashMap<>(); |
| 32 | + for (String s : input) { |
| 33 | + Transaction trans = new Transaction(s); |
| 34 | + if (!map.containsKey(trans.name)) { |
| 35 | + map.put(trans.name, new ArrayList<>()); |
33 | 36 | }
|
34 |
| - for (Map.Entry<Integer, Transaction> entry : |
35 |
| - map.get(name).subMap(time - 60, time + 60).entrySet()) { |
36 |
| - if (Math.abs(time - entry.getKey()) <= 60 && !entry.getValue().city.equals(city)) { |
37 |
| - result.add(transaction); |
38 |
| - break; |
39 |
| - } |
| 37 | + map.get(trans.name).add(trans); |
| 38 | + } |
| 39 | + for (String s : input) { |
| 40 | + Transaction trans = new Transaction(s); |
| 41 | + if (!isValid(trans, map)) { |
| 42 | + res.add(s); |
40 | 43 | }
|
41 | 44 | }
|
42 |
| - return result; |
| 45 | + return res; |
43 | 46 | }
|
44 | 47 |
|
45 |
| - private static class Transaction { |
46 |
| - int amount; |
47 |
| - String city; |
48 |
| - |
49 |
| - public Transaction(int amount, String city) { |
50 |
| - this.amount = amount; |
51 |
| - this.city = city; |
| 48 | + private boolean isValid(Transaction transaction, Map<String, List<Transaction>> map) { |
| 49 | + if (transaction.amount > 1000) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + for (Transaction s : map.get(transaction.name)) { |
| 53 | + if (Math.abs(s.time - transaction.time) <= 60 && !s.city.equals(transaction.city)) { |
| 54 | + return false; |
| 55 | + } |
52 | 56 | }
|
| 57 | + return true; |
53 | 58 | }
|
54 | 59 | }
|
0 commit comments