【leetcode】1169. 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.

解题思路:题目不难,但是坑多。我的方法是以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

 

转载于:https://www.cnblogs.com/seyjs/p/11440765.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值