Can somebody find error in my code


  • 0
    P

    The code is pretty straight forward, o(n^2) algorithm as others. But it gives me WA for input:
    [-1,1,1,1,-1]
    [0,-1,-1,0,1]
    [-1,-1,1,-1,-1]
    [0,1,0,-1,-1]

    Thanks in advance.

    def fourSumCount(self, A, B, C, D):

           def twoSum(A,B, mul=1):
            results = {}
            for a in A:
                for b in B:
                    results[mul*(a+b)] = results.get(mul*(a+b),0) + 1
            return results
        
        ABsum = twoSum(A,B,1)
        CDsum = twoSum(C,D,-1)
        
        count = 0
        for ab in ABsum.keys():
            if -1*ab in CDsum.keys():
                count +=  (ABsum[ab] * CDsum[-1*ab])
        return count

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.