-
Notifications
You must be signed in to change notification settings - Fork 0
/
454_4Sum_II.cpp
33 lines (29 loc) · 1004 Bytes
/
454_4Sum_II.cpp
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
class Solution {
public:
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
// Brute force : time complexity = O(n^4)
// Hash : time complexity = O(n^2 + n^2)
unordered_map<int,int> map_sum1_2;
unordered_map<int,int> map_sum3_4;
int len = nums1.size();
int ans = 0;
// O(n^2)
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
int sum = nums1[i] + nums2[j];
map_sum1_2[sum]++;
}
}
// O(n^2)
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
int sum = nums3[i] + nums4[j];
//if (map_sum1_2.count(0 - sum) == 1) {
if (map_sum1_2.find(0 - sum) != map_sum1_2.end()) {
ans += (map_sum1_2[0 - sum]);
}
}
}
return ans;
}
};