leetcode :hashtable 454. 4Sum II

topic

Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.

To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.

Example:

Input:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]

Output:
2

Explanation:
The two tuples are:
1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0

Thinking

Violence: separately through each array

answer

class Solution:
    def fourSumCount(self, A, B,C,D) -> int:
        def twoSumCount(target):
            count = 0
            h = 0
            k = len(D) - 1
            while h<len(C) and k>=0:
                sumNum = C[h] + D[k]
                if sumNum<target:
                    h += 1
                    while h<len(C) and C[h] == C[h-1]:
                        h += 1
                elif sumNum>target:
                    k -= 1
                    while k>=0 and D[k] == D[k+1]:
                        k-=1
                else:
                    c1 = 1
                    h += 1
                    while h<len(C) and C[h] == C[h-1]:
                        h += 1
                        c1 += 1
                    c2 = 1
                    k -= 1
                    while k>=0 and D[k] == D[k+1]:
                        k-=1
                        c2 += 1
                    count += c1*c2
            return count
        
        n = len(A)
        A,B,C,D = sorted(A),sorted(B),sorted(C),sorted(D)
        count = 0
        target = 0
        i = 0
        while i < n:
            count2 = 0
            j = 0
            while j < n:
                count_temp = twoSumCount(target-A[i]-B[j]) 
                j += 1
                c2 = 1
                if count_temp >0:
                    while j<n and B[j]==B[j-1]:
                        j += 1
                        c2 += 1
                    count2 += count_temp*c2
            i += 1
            c1 = 1
            if count2:
                while i<n and A[i]==A[i-1]:
                    i += 1
                    c1 += 1
                count += count2*c1
            
                    
        return count

 

This time out, and this can be used to reduce the time hashtable. 4Sum only problem is different from an array, the array is four. So basically the two numbers plus and it has been the same. I do not always count again, beginning to reckon, there is a table. Then pick up just fine.

class Solution:
    def fourSumCount(self, A, B,C,D) -> int:
        AB = collections.defaultdict(lambda:0)
        for a in A:
            for b in B:
                AB[a+b] += 1
        count = 0
        keys = AB.keys()
        for c in C:
            for d in D:
                if -c-d in keys:
                    count += AB[-c-d]
            
        return count

 

发布了45 篇原创文章 · 获赞 1 · 访问量 3371

Guess you like

Origin blog.csdn.net/qq_22498427/article/details/104504813