1169. Invalid Transactions

本来想维护一个 last transaction,写完发现transactions并不是chronological排列的,想着sort一下再做应该就行了,然后意识到给的是string不是list,没法这样sort,整个重写。

Brute Force 就直接map function然后 O(N**2) 过,即便这么粗暴竟然也 beat 84%...

看看 discuss 和网上的解答,的确都是O(N**2)的解法,自己试了下双指针解,本质上没有downsize多少运算,还会被各种 corner case 困住。

class Solution:
    def invalidTransactions(self, transactions: List[str]) -> List[str]:
        res = set()
        transactions = list(map(lambda x: x.split(','), transactions))
        for t in transactions:
            if int(t[2]) > 1000:
                res.add(','.join(t))
                continue
            for c in transactions:
                if t[0] == c[0] and abs(int(t[1])-int(c[1])) <= 60 and t[3] != c[3]:
                    res.add(','.join(t))
                    break
        return res

Last updated

Was this helpful?