454. 4Sum II

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




// brute force n ^ 4 
// tle 


class Solution {
    public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
        int res = 0;
        // brute force way: try all possible tuples and decide 
        int n = A.length;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                for(int k = 0; k < n; k++){
                    for(int l = 0; l < n; l++){
                        int sum = A[i] + B[j] + C[k] + D[l];
                        if(sum == 0) res++;
                    }
                }
            }
        }
        return res;
        
        
    }
}


// sol 2 : use HashMap

// keySet()
// values()

Same idea but much cleaner code 
https://leetcode.com/problems/4sum-ii/discuss/93920/Clean-java-solution-O(n2)


class Solution {
    public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
        // n ^ 2 
        // use hashset to contains the half sum 
        // when compute the other half, check if the hashset contains the 0 - this half sum 
        // problem, duplicate sum but differnt pairs of combo
        // use hashmap, key is the sum value, value is the freq of that sum 
        int res = 0;
        int n = A.length;
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                int sum = A[i] + B[j];
                if(!map.containsKey(sum)){
                    map.put(sum, 1);
                }else{
                    int freq = map.get(sum);
                    map.put(sum, freq + 1);
                }
            }
        }
        HashMap<Integer, Integer> map2 = new HashMap<>();
        for(int l = 0; l < n; l++){
            for(int r = 0; r < n; r++){
                int sum = C[l] + D[r];
                if(!map2.containsKey(sum)){
                    map2.put(sum, 1);
                }else{
                    int freq = map2.get(sum);
                    map2.put(sum, freq + 1);
                }
            }
        }
        for(int key : map.keySet()){
            if(map2.containsKey(0 - key)){
                res += map.get(key) * map2.get(0 - key);
            }
            
        }
        return res;
    }
}

 

public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
    Map<Integer, Integer> map = new HashMap<>();
    
    for(int i=0; i<C.length; i++) {
        for(int j=0; j<D.length; j++) {
            int sum = C[i] + D[j];
            map.put(sum, map.getOrDefault(sum, 0) + 1);
        }
    }
    
    int res=0;
    for(int i=0; i<A.length; i++) {
        for(int j=0; j<B.length; j++) {
            res += map.getOrDefault(-1 * (A[i]+B[j]), 0);
        }
    }
    
    return res;
}

Time complexity:  O(n^2)
Space complexity: O(n^2)

 

posted on 2018-11-09 10:18  猪猪&#128055;  阅读(143)  评论(0)    收藏  举报

导航