http://bookshadow.com/weblog/2016/11/13/leetcode-4sum-ii/
利用字典cnt,将A,B中各元素(笛卡尔积)的和进行分类计数。
https://discuss.leetcode.com/topic/67659/easy-2-lines-o-n-2-python/
http://www.cnblogs.com/grandyang/p/6073317.html
这种方法用了两个哈希表分别记录AB和CB的两两之和出现次数,然后遍历其中一个哈希表,并在另一个哈希表中找和的相反数出现的次数
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:
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)
https://discuss.leetcode.com/topic/68168/dividing-arrays-into-two-parts-full-thinking-process-from-naive-n-4-to-effective-n-2-solution
- The naive solution is to run four loops by iterating all elements and check for (A[i] + B[j] + C[k] + d[h]) == 0. Time complexity: N^4
- We can improve solution by iterating through elements of three arrays and check if the fourth array contains A[i] + B[j] + C[k] + d == 0 ----> d = -A[i] - B[j] - C[k]. We can use HashSet to store elements of fourth array. Overall time complexity: N^3;
- To improve the solution we can divide arrays into two parts. Then make calculation of sums of one part (A[i] + B[j]) and store their sum's occurences counter in a HashMap. While calculating second part arrays' sum (secondSum = C[k] + D[h]) we can check whether map contains secondSum*(-1);
A[i] + B[j] == - C[k] - D[h]
A[i] + B[j] == - (C[k]+D[h])
This solution can be extended for N arrays.
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
HashMap<Integer, Integer> sumCounter = getSumCounters(A,B);
int fourSumCounter = 0;
for (int c : C) {
for (int d: D) {
fourSumCounter += sumCounter.getOrDefault(c+d, 0);
}
}
return fourSumCounter;
}
private HashMap<Integer, Integer> getSumCounters(int [] A, int [] B) {
HashMap<Integer, Integer> sumCounter = new HashMap<>();
for (int a : A) {
for (int b: B) {
int sum = -a-b;
sumCounter.put(sum, sumCounter.getOrDefault(sum, 0) + 1);
}
}
return sumCounter;
}
https://discuss.leetcode.com/topic/67658/simple-java-solution-with-explanation
Take the arrays A and B, and compute all the possible sums of two elements. Put the sum in the Hash map, and increase the hash map value if more than 1 pair sums to the same value.
Compute all the possible sums of the arrays C and D. If the hash map contains the opposite value of the current sum, increase the count of four elements sum to 0 by the counter in the map.
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
Map<Integer,Integer> sums = new HashMap<>();
int count = 0;
for(int i=0; i<A.length;i++) {
for(int j=0;j<B.length;j++){
int sum = A[i]+B[j];
if(sums.containsKey(sum)) {
sums.put(sum, sums.get(sum)+1);
} else {
sums.put(sum, 1);
}
}
}
for(int k=0; k<C.length;k++) {
for(int z=0;z<D.length;z++){
int sum = -(C[k]+D[z]);
if(sums.containsKey(sum)) {
count+=sums.get(sum);
}
}
}
return count;
}
利用字典cnt,将A,B中各元素(笛卡尔积)的和进行分类计数。
将C,D中各元素(笛卡尔积)和的相反数在cnt中的值进行累加,即为答案。
def fourSumCount(self, A, B, C, D):
"""
:type A: List[int]
:type B: List[int]
:type C: List[int]
:type D: List[int]
:rtype: int
"""
ans = 0
cnt = collections.defaultdict(int)
for a in A:
for b in B:
cnt[a + b] += 1
for c in C:
for d in D:
ans += cnt[-(c + d)]
return anshttps://discuss.leetcode.com/topic/67659/easy-2-lines-o-n-2-python/
def fourSumCount(self, A, B, C, D):
AB = collections.Counter(a+b for a in A for b in B)
return sum(AB[-c-d] for c in C for d in D)
X.http://www.cnblogs.com/grandyang/p/6073317.html
这种方法用了两个哈希表分别记录AB和CB的两两之和出现次数,然后遍历其中一个哈希表,并在另一个哈希表中找和的相反数出现的次数
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) { int res = 0, n = A.size(); unordered_map<int, int> m1, m2; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { ++m1[A[i] + B[j]]; ++m2[C[i] + D[j]]; } } for (auto a : m1) res += a.second * m2[-a.first]; return res; }