Skip to content

Commit 6459bce

Browse files
committed
feat: add solutions to lc problem: No.1385
No.1385: Find the Distance Value Between Two Arrays
1 parent e2c92c6 commit 6459bce

File tree

7 files changed

+119
-378
lines changed

7 files changed

+119
-378
lines changed

solution/1300-1399/1385.Find the Distance Value Between Two Arrays/README.md

Lines changed: 39 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,11 @@
6666

6767
<!-- 这里可写通用的实现逻辑 -->
6868

69-
**方法一:暴力枚举**
69+
**方法一:排序 + 二分查找**
7070

71-
由于 `arr1``arr2` 的长度不超过 500,因此可以直接暴力遍历
71+
我们可以先对数组 $arr2$ 排序,然后对于数组 $arr1$ 中的每个元素 $a$,使用二分查找,找到数组 $arr2$ 中第一个大于等于 $a-d$ 的元素,如果元素存在,且小于等于 $a+d$,则说明不符合距离要求,否则说明符合距离要求。我们将符合距离要求的元素个数累加,即为答案
7272

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$ 的长度。
8274

8375
<!-- tabs:start -->
8476

@@ -89,17 +81,9 @@
8981
```python
9082
class Solution:
9183
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
10387

10488
arr2.sort()
10589
return sum(check(a) for a in arr1)
@@ -109,29 +93,6 @@ class Solution:
10993

11094
<!-- 这里可写当前语言的特殊实现逻辑 -->
11195

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-
13596
```java
13697
class Solution {
13798
public int findTheDistanceValue(int[] arr1, int[] arr2, int d) {
@@ -146,19 +107,16 @@ class Solution {
146107
}
147108

148109
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;
152113
if (arr[mid] >= a - d) {
153-
right = mid;
114+
r = mid;
154115
} else {
155-
left = mid + 1;
116+
l = mid + 1;
156117
}
157118
}
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;
162120
}
163121
}
164122
```
@@ -169,89 +127,32 @@ class Solution {
169127
class Solution {
170128
public:
171129
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+
};
191134
sort(arr2.begin(), arr2.end());
192135
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+
}
196139
return ans;
197140
}
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-
}
204141
};
205142
```
206143
207144
### **Go**
208145
209146
```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) {
232148
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
249149
for _, a := range arr1 {
250-
if check(a) {
150+
i := sort.SearchInts(arr2, a-d)
151+
if i == len(arr2) || arr2[i] > a+d {
251152
ans++
252153
}
253154
}
254-
return ans
155+
return
255156
}
256157
```
257158

@@ -263,64 +164,32 @@ function findTheDistanceValue(
263164
arr2: number[],
264165
d: number,
265166
): 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;
292174
} else {
293-
right = mid;
175+
l = mid + 1;
294176
}
295177
}
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;
301185
}
302-
res++;
303186
}
304-
return res;
187+
return ans;
305188
}
306189
```
307190

308191
### **Rust**
309192

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-
324193
```rust
325194
impl Solution {
326195
pub fn find_the_distance_value(arr1: Vec<i32>, mut arr2: Vec<i32>, d: i32) -> i32 {

0 commit comments

Comments
 (0)