码迷,mamicode.com
首页 > 其他好文 > 详细

【leetcode】1169. Invalid Transactions

时间:2019-08-31 23:39:17      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:记录   lan   The   mount   within   ssi   int   pen   bec   

题目如下:

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.

解题思路:题目不难,但是坑多。我的方法是以name为key值,把每个人的交易记录存入字典中,然后遍历每个人的交易记录。每找到一个不合法的记录,再用这个记录和这个人其他所有的记录比较,直到找出所有符合条件的结果为止。

代码如下:

class Solution(object):
    def invalidTransactions(self, transactions):
        """
        :type transactions: List[str]
        :rtype: List[str]
        """
        def cmpf(v1,v2):
            lv1 = v1.split(,)
            lv2 = v2.split(,)
            return int(lv1[1]) - int(lv2[1])
        transactions.sort(cmp = cmpf)

        res = []

        dic = {}

        dic_invalid = {}

        for transaction in transactions:
            if transaction == lee,158,987,mexico:
                pass
            n, t, a, c = transaction.split(",")
            if int(a) > 1000:
                if transaction not in dic_invalid:
                    res.append(transaction)
                    dic_invalid[transaction] = 1
            if n in dic:
                for hn,ht,ha,hc in dic[n]:
                    if hc != c and (int(t) - int(ht)) <= 60:
                        tran = hn + , + ht + , + ha + , + hc
                        if tran not in dic_invalid:
                            dic_invalid[tran] = 1
                            res.append(tran)
                        if transaction not in dic_invalid:
                            res.append(transaction)
                            dic_invalid[transaction] = 1
            dic[n] = dic.setdefault(n,[]) + [(n,t,a,c)]
        return res

 

【leetcode】1169. Invalid Transactions

标签:记录   lan   The   mount   within   ssi   int   pen   bec   

原文地址:https://www.cnblogs.com/seyjs/p/11440765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!