66
66
67
67
<!-- 这里可写通用的实现逻辑 -->
68
68
69
- ** 方法一:暴力枚举 **
69
+ ** 方法一:排序 + 二分查找 **
70
70
71
- 由于 ` arr1 ` 和 ` arr2 ` 的长度不超过 500,因此可以直接暴力遍历 。
71
+ 我们可以先对数组 $arr2$ 排序,然后对于数组 $ arr1$ 中的每个元素 $a$,使用二分查找,找到数组 $ arr2$ 中第一个大于等于 $a-d$ 的元素,如果元素存在,且小于等于 $a+d$,则说明不符合距离要求,否则说明符合距离要求。我们将符合距离要求的元素个数累加,即为答案 。
72
72
73
- 时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别为 ` arr1 ` 和 ` arr2 ` 的长度。
74
-
75
- ** 方法二:二分查找**
76
-
77
- 对于 ` arr1 ` 中的每个元素 ` a ` ,若在 ` arr2 ` 中存在 ` b ` ,使得 ` b ∈ [a - d, a + d] ` ,那么就符合距离要求,不进行累加。
78
-
79
- 因此,可以先对 ` arr2 ` 进行排序。然后对于每个元素 ` a ` ,二分枚举 ` arr2 ` 判断是否存在符合距离要求的 ` b ` 。
80
-
81
- 时间复杂度 $O((m + n) \times \log n)$。
73
+ 时间复杂度 $O((m + n) \times \log n)$,空间复杂度 $O(\log n)$。其中 $m$ 和 $n$ 分别是数组 $arr1$ 和 $arr2$ 的长度。
82
74
83
75
<!-- tabs:start -->
84
76
89
81
``` python
90
82
class Solution :
91
83
def findTheDistanceValue (self , arr1 : List[int ], arr2 : List[int ], d : int ) -> int :
92
- return sum (all (abs (a - b) > d for b in arr2) for a in arr1)
93
- ```
94
-
95
- ``` python
96
- class Solution :
97
- def findTheDistanceValue (self , arr1 : List[int ], arr2 : List[int ], d : int ) -> int :
98
- def check (a ):
99
- idx = bisect_left(arr2, a - d)
100
- if idx != len (arr2) and arr2[idx] <= a + d:
101
- return False
102
- return True
84
+ def check (a : int ) -> bool :
85
+ i = bisect_left(arr2, a - d)
86
+ return i == len (arr2) or arr2[i] > a + d
103
87
104
88
arr2.sort()
105
89
return sum (check(a) for a in arr1)
@@ -109,29 +93,6 @@ class Solution:
109
93
110
94
<!-- 这里可写当前语言的特殊实现逻辑 -->
111
95
112
- ``` java
113
- class Solution {
114
- public int findTheDistanceValue (int [] arr1 , int [] arr2 , int d ) {
115
- int ans = 0 ;
116
- for (int a : arr1) {
117
- if (check(arr2, a, d)) {
118
- ++ ans;
119
- }
120
- }
121
- return ans;
122
- }
123
-
124
- private boolean check (int [] arr , int a , int d ) {
125
- for (int b : arr) {
126
- if (Math . abs(a - b) <= d) {
127
- return false ;
128
- }
129
- }
130
- return true ;
131
- }
132
- }
133
- ```
134
-
135
96
``` java
136
97
class Solution {
137
98
public int findTheDistanceValue (int [] arr1 , int [] arr2 , int d ) {
@@ -146,19 +107,16 @@ class Solution {
146
107
}
147
108
148
109
private boolean check (int [] arr , int a , int d ) {
149
- int left = 0 , right = arr. length;
150
- while (left < right ) {
151
- int mid = (left + right ) >> 1 ;
110
+ int l = 0 , r = arr. length;
111
+ while (l < r ) {
112
+ int mid = (l + r ) >> 1 ;
152
113
if (arr[mid] >= a - d) {
153
- right = mid;
114
+ r = mid;
154
115
} else {
155
- left = mid + 1 ;
116
+ l = mid + 1 ;
156
117
}
157
118
}
158
- if (left != arr. length && arr[left] <= a + d) {
159
- return false ;
160
- }
161
- return true ;
119
+ return l >= arr. length || arr[l] > a + d;
162
120
}
163
121
}
164
122
```
@@ -169,89 +127,32 @@ class Solution {
169
127
class Solution {
170
128
public:
171
129
int findTheDistanceValue(vector<int >& arr1, vector<int >& arr2, int d) {
172
- int ans = 0;
173
- for (int& a : arr1)
174
- ans += check(arr2, a, d);
175
- return ans;
176
- }
177
-
178
- bool check(vector<int>& arr, int a, int d) {
179
- for (int& b : arr)
180
- if (abs(a - b) <= d)
181
- return false;
182
- return true;
183
- }
184
- };
185
- ```
186
-
187
- ``` cpp
188
- class Solution {
189
- public:
190
- int findTheDistanceValue(vector<int >& arr1, vector<int >& arr2, int d) {
130
+ auto check = [ &] (int a) -> bool {
131
+ auto it = lower_bound(arr2.begin(), arr2.end(), a - d);
132
+ return it == arr2.end() || * it > a + d;
133
+ };
191
134
sort(arr2.begin(), arr2.end());
192
135
int ans = 0;
193
- for (int& a : arr1)
194
- if ( check(arr2, a, d))
195
- ++ans;
136
+ for (int& a : arr1) {
137
+ ans += check(a);
138
+ }
196
139
return ans;
197
140
}
198
-
199
- bool check(vector<int>& arr, int a, int d) {
200
- int idx = lower_bound(arr.begin(), arr.end(), a - d) - arr.begin();
201
- if (idx != arr.size() && arr[idx] <= a + d) return false;
202
- return true;
203
- }
204
141
};
205
142
```
206
143
207
144
### **Go**
208
145
209
146
```go
210
- func findTheDistanceValue (arr1 []int , arr2 []int , d int ) int {
211
- check := func (arr []int , a int ) bool {
212
- for _ , b := range arr {
213
- if -d <= a-b && a-b <= d {
214
- return false
215
- }
216
- }
217
- return true
218
- }
219
-
220
- ans := 0
221
- for _ , a := range arr1 {
222
- if check (arr2, a) {
223
- ans++
224
- }
225
- }
226
- return ans
227
- }
228
- ```
229
-
230
- ``` go
231
- func findTheDistanceValue (arr1 []int , arr2 []int , d int ) int {
147
+ func findTheDistanceValue(arr1 []int, arr2 []int, d int) (ans int) {
232
148
sort.Ints(arr2)
233
- check := func (a int ) bool {
234
- left , right := 0 , len (arr2)
235
- for left < right {
236
- mid := (left + right) >> 1
237
- if arr2[mid] >= a-d {
238
- right = mid
239
- } else {
240
- left = mid + 1
241
- }
242
- }
243
- if left != len (arr2) && arr2[left] <= a+d {
244
- return false
245
- }
246
- return true
247
- }
248
- ans := 0
249
149
for _, a := range arr1 {
250
- if check (a) {
150
+ i := sort.SearchInts(arr2, a-d)
151
+ if i == len(arr2) || arr2[i] > a+d {
251
152
ans++
252
153
}
253
154
}
254
- return ans
155
+ return
255
156
}
256
157
```
257
158
@@ -263,64 +164,32 @@ function findTheDistanceValue(
263
164
arr2 : number [],
264
165
d : number ,
265
166
): number {
266
- let res = 0 ;
267
- for (const num of arr1 ) {
268
- if (arr2 .every (v => Math .abs (num - v ) > d )) {
269
- res ++ ;
270
- }
271
- }
272
- return res ;
273
- }
274
- ```
275
-
276
- ``` ts
277
- function findTheDistanceValue(
278
- arr1 : number [],
279
- arr2 : number [],
280
- d : number ,
281
- ): number {
282
- arr2 .sort ((a , b ) => a - b );
283
- const n = arr2 .length ;
284
- let res = 0 ;
285
- for (const num of arr1 ) {
286
- let left = 0 ;
287
- let right = n - 1 ;
288
- while (left < right ) {
289
- const mid = (left + right ) >>> 1 ;
290
- if (arr2 [mid ] <= num ) {
291
- left = mid + 1 ;
167
+ const check = (a : number ) => {
168
+ let l = 0 ;
169
+ let r = arr2 .length ;
170
+ while (l < r ) {
171
+ const mid = (l + r ) >> 1 ;
172
+ if (arr2 [mid ] >= a - d ) {
173
+ r = mid ;
292
174
} else {
293
- right = mid ;
175
+ l = mid + 1 ;
294
176
}
295
177
}
296
- if (
297
- Math .abs (num - arr2 [left ]) <= d ||
298
- (left !== 0 && Math .abs (num - arr2 [left - 1 ]) <= d )
299
- ) {
300
- continue ;
178
+ return l === arr2 .length || arr2 [l ] > a + d ;
179
+ };
180
+ arr2 .sort ((a , b ) => a - b );
181
+ let ans = 0 ;
182
+ for (const a of arr1 ) {
183
+ if (check (a )) {
184
+ ++ ans ;
301
185
}
302
- res ++ ;
303
186
}
304
- return res ;
187
+ return ans ;
305
188
}
306
189
```
307
190
308
191
### ** Rust**
309
192
310
- ``` rust
311
- impl Solution {
312
- pub fn find_the_distance_value (arr1 : Vec <i32 >, arr2 : Vec <i32 >, d : i32 ) -> i32 {
313
- let mut res = 0 ;
314
- for num in arr1 . iter () {
315
- if arr2 . iter (). all (| v | i32 :: abs (num - v ) > d ) {
316
- res += 1 ;
317
- }
318
- }
319
- res
320
- }
321
- }
322
- ```
323
-
324
193
``` rust
325
194
impl Solution {
326
195
pub fn find_the_distance_value (arr1 : Vec <i32 >, mut arr2 : Vec <i32 >, d : i32 ) -> i32 {
0 commit comments