zoukankan      html  css  js  c++  java
  • 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 between 1 and 10.
    • Each {time} consist of digits, and represent an integer between 0 and 1000.
    • Each {amount} consist of digits, and represent an integer between 0 and 2000.

    题解:

    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:

     1 class Solution {
     2     public List<String> invalidTransactions(String[] transactions) {
     3         if(transactions == null || transactions.length == 0){
     4             return new ArrayList<String>();
     5         }
     6         
     7         Arrays.sort(transactions, (a, b) -> {
     8             String [] aArr = a.split(",");
     9             String [] bArr = b.split(",");
    10             
    11             return Integer.valueOf(aArr[1]) - Integer.valueOf(bArr[1]);
    12         });
    13         
    14         HashSet<String> res = new HashSet<>();
    15         HashMap<String, LinkedList<Node>> hm = new HashMap<>();
    16         for(String t : transactions){
    17             String [] parts = t.split(",");
    18             if(Integer.valueOf(parts[2]) > 1000){
    19                 res.add(t);
    20             }
    21             
    22             int time = Integer.valueOf(parts[1]);
    23             String city = parts[3];
    24             
    25             hm.putIfAbsent(parts[0], new LinkedList<Node>());
    26             LinkedList<Node> item = hm.get(parts[0]);
    27             
    28             while(!item.isEmpty() && time - item.peekFirst().time > 60){
    29                 item.pollFirst();
    30             }
    31             
    32             boolean issueFound = false;
    33             for(Node n : item){
    34                 if(!n.city.equals(city)){
    35                     res.add(parts[0] + "," + n.time + "," + n.amount + "," + n.city);
    36                     res.add(t);
    37                 }
    38             }
    39             
    40             if(!issueFound){
    41                 item.add(new Node(time, city, parts[2]));
    42             }
    43         }
    44         
    45         return new ArrayList<String>(res);
    46     }
    47 }
    48 
    49 class Node {
    50     int time;
    51     String city;
    52     String amount;
    53     public Node(int time, String city, String amount){
    54         this.time = time;
    55         this.city = city;
    56         this.amount = amount;
    57     }
    58 }
  • 相关阅读:
    MySQL数据模型
    Spring循环依赖
    @Autowired和@Resource区别
    Kafka概念
    阻塞队列
    线程池原理
    Spring AOP
    JVM 史上最最最完整深入解析(12000 字噢)
    Dubbo配置信息
    友情链接
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12321352.html
Copyright © 2011-2022 走看看