-
Notifications
You must be signed in to change notification settings - Fork 2
/
Sum4II.java
84 lines (75 loc) · 1.72 KB
/
Sum4II.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.algorithm.playground.leetcode.problems.lc400.lc450.lc454;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* https://leetcode.com/problems/4sum-ii/description/
*/
public class Sum4II {
class Solution {
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
int count = 0;
int N = A.length;
int[] AB = new int[N * N];
int[] CD = new int[N * N];
int idx = 0;
Map<Integer, Integer> counts = new HashMap<>();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++, idx++) {
AB[idx] = A[i] + B[j];
CD[idx] = C[i] + D[j];
counts.put(CD[idx], counts.getOrDefault(CD[idx], 0) + 1);
}
}
Arrays.sort(AB);
Arrays.sort(CD);
int prevVal = Integer.MIN_VALUE;
int prevScore = 0;
for (int a : AB) {
if (a != prevVal) {
prevVal = a;
int j = binarySearch(CD, a);
if (j != -1) {
prevScore = counts.get(CD[j]);
} else {
prevScore = 0;
}
}
count += prevScore;
}
return count;
}
private int binarySearch(int[] nums, int curr) {
int l = 0, r = nums.length - 1;
while (l < r) {
int mid = l + (r - l) / 2;
if (curr + nums[mid] < 0) {
l = mid + 1;
} else if (curr + nums[mid] == 0) {
return mid;
} else {
r = mid;
}
}
return curr + nums[l] == 0 ? l : -1;
}
}
class FastSolution {
public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
Map<Integer, Integer> map = new HashMap<>();
for (int a : A) {
for (int b : B) {
int sum = a + b;
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
}
int res = 0;
for (int c : C) {
for (int d : D) {
res += map.getOrDefault(0 - c - d, 0);
}
}
return res;
}
}
}