Problem statement

https://leetcode.com/problems/invalid-transactions/

Solution

Just check every pair to see if we have invalid transaction. Also there are border cases, where we have equal elements: we need to count them several times.

Complexity

It is O(n^2) for time and O(n) for space.

Code

class Solution:
    def invalidTransactions(self, T):
        ans = set()
        cnt = Counter(T)
        for x1, x2 in combinations(T, 2):
            n1, t1, a1, c1 = x1.split(",")
            n2, t2, a2, c2 = x2.split(",")
            if n1 == n2 and abs(int(t2) - int(t1)) <= 60 and c1 != c2:
                ans.add(x1)
                ans.add(x2)
        
        for x in T:
            n1, t1, a1, c1 = x.split(",")
            if int(a1) > 1000: ans.add(x)
                
        return chain(*[[x] * cnt[x] for x in ans])