forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1385.java
More file actions
26 lines (23 loc) · 644 Bytes
/
1385.java
File metadata and controls
26 lines (23 loc) · 644 Bytes
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
class Solution {
public int findTheDistanceValue(int[] arr1, int[] arr2, int d) {
Arrays.sort(arr2);
int res = 0;
this.d = d;
for (int x : arr1) {
if (check(arr2, x)) res++;
}
return res;
}
private int d;
private boolean check(int[] arr2, int x) {
int l = 0;
int r = arr2.length - 1;
while (l < r) {
int mid = (l + r) >> 1;
if (Math.abs(arr2[mid] - x) <= d) return false;
if (arr2[mid] > x) r = mid;
else l = mid + 1;
}
return Math.abs(arr2[l] - x) > d;
}
}