Invalid Transactions

Problem Id: 1169 Difficulty: Medium


Intuition

Solution


class Solution:
    def invalidTransactions(self, transactions: List[str]) -> List[str]:
        users = {}
        ans = []
        for tran in transactions:
            t = tran.split(',')
            t[1] = int(t[1])
            t[2] = int(t[2])
            if t[0] not in users:
                users[t[0]] = []
            users[t[0]].append(t[1:])
        for k in users:
            users[k].sort()
        for u in users:
            trans = users[u]
            for i in range(len(trans)):
                if trans[i][1] > 1000:
                    ans.append("%s,%d,%d,%s" % (u, trans[i][0], trans[i][1], trans[i][2]))
                else:
                    for j in range(len(trans)):
                        if j == i:
                            continue
                        if j >= 0 and abs(trans[j][0] - trans[i][0]) <= 60 and trans[j][2] != trans[i][2]:
                            ans.append("%s,%d,%d,%s" % (u, trans[i][0], trans[i][1], trans[i][2]))
                            break
        return ans