-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathfind-the-distance-value-between-two-arrays.cpp
More file actions
36 lines (34 loc) · 1.11 KB
/
find-the-distance-value-between-two-arrays.cpp
File metadata and controls
36 lines (34 loc) · 1.11 KB
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
// Time: O((n + m) * logm)
// Space: O(1)
class Solution {
public:
int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
sort(begin(arr2), end(arr2));
int result = 0;
for (const auto& x : arr1) {
auto j = distance(cbegin(arr2), lower_bound(cbegin(arr2), cend(arr2), x));
const auto& left = (j - 1 >= 0) ? arr2[j - 1] : numeric_limits<int>::min();
const auto& right = (j < arr2.size()) ? arr2[j] : numeric_limits<int>::max();
result += left + d < x && x < right - d;
}
return result;
}
};
// Time: O(nlogn + mlogm)
// Space: O(1)
class Solution2 {
public:
int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
sort(begin(arr1), end(arr1)), sort(begin(arr2), end(arr2));
int result = 0, i = 0;
for (int j = 0; i < arr1.size() && j < arr2.size();) {
if (arr1[i] - arr2[j] > d) {
++j;
continue;
}
result += arr2[j] - arr1[i] > d;
++i;
}
return result + arr1.size() - i;
}
};