[454] LeetCode four and the number of II

Level Title: 4Sum II (Medium)

Subject description:

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

  The meaning of problems: Given integer array containing a list of four A, B, C, D, to calculate how many tuples (i, j, k, l ), such that A [i] + B [j ] + C [k ] + D [l] = 0 .


Problem-solving ideas:

  This is a problem before [18] LeetCode, and four the number of extension, except that the number four is not looking from an array, but from the same number and find a four array length, and does not require us to record number corresponding to just count the number.

  Only the number of statistics to satisfy the conditions a little simpler, if violence law requires, traversing an array of four, four-cycle needs, time complexity is too high, here or return to our initial ideas to solve the two numbers are up, with space for time, still use HashMap.

  The idea is: four array divided into two parts, two of a group, all elements in the sum of two arrays of two and two, and the number will be stored as they appear in the hash table, the hash table so long as the two find each other in case of opposite number, you can get all times.

  When Still further, can first summation elements A and B, and then the number stored in the hash table, and then traverse the sum of the elements C and D, as long as appear in the hash table of A + B the number corresponding to the opposite, and that is to find a set of solutions, this can save a hash table space, while also eliminating the need for overhead and finally compare two hash table.

  Not difficult to understand, with reference to the following code:

class Solution {
    public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
        Map<Integer,Integer> a_And_b=new HashMap<>(); //<a和b的和,出现次数>
        for(int a:A){  //两层循环,将A和B元素之和及其出现次数保存到哈希表
            for(int b:B){
                a_And_b.put(a+b,a_And_b.getOrDefault(a+b,0)+1);
            }
        }
        //然后再遍历C和D,两两元素求和,只要和的相反数在哈希表a_And_b中出现了几次,即说明有几次为0
        int res=0;
        for(int c:C){
            for(int d:D){
                int target=-1*(c+d); //c+d的相反数
                if(a_And_b.containsKey(target))
                    res+=a_And_b.get(target);
            }
        }
        return res;
    }
}

  Time complexity: O (n ^ 2), the spatial complexity: the cost of a hash table, at most O (2n)

to sum up

  This question is quite interesting, especially the use of the hash table is very flexible.

Guess you like

Origin www.cnblogs.com/gzshan/p/11129770.html