Skip to content

Commit 70ce676

Browse files
authoredDec 5, 2024··
feat: add solutions to lc problem: No.1385 (#3840)
No.1385.Find the Distance Value Between Two Arrays
1 parent 3a2b887 commit 70ce676

File tree

8 files changed

+118
-217
lines changed

8 files changed

+118
-217
lines changed
 

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

+41-74
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ tags:
3333
<strong>输出:</strong>2
3434
<strong>解释:</strong>
3535
对于 arr1[0]=4 我们有:
36-
|4-10|=6 &gt; d=2
37-
|4-9|=5 &gt; d=2
38-
|4-1|=3 &gt; d=2
39-
|4-8|=4 &gt; d=2
36+
|4-10|=6 &gt; d=2
37+
|4-9|=5 &gt; d=2
38+
|4-1|=3 &gt; d=2
39+
|4-8|=4 &gt; d=2
4040
所以 arr1[0]=4 符合距离要求
4141

4242
对于 arr1[1]=5 我们有:
43-
|5-10|=5 &gt; d=2
44-
|5-9|=4 &gt; d=2
45-
|5-1|=4 &gt; d=2
43+
|5-10|=5 &gt; d=2
44+
|5-9|=4 &gt; d=2
45+
|5-1|=4 &gt; d=2
4646
|5-8|=3 &gt; d=2
4747
所以 arr1[1]=5 也符合距离要求
4848

@@ -51,7 +51,7 @@ tags:
5151
<strong>|8-9|=1 &lt;= d=2</strong>
5252
|8-1|=7 &gt; d=2
5353
<strong>|8-8|=0 &lt;= d=2</strong>
54-
存在距离小于等于 2 的情况,不符合距离要求
54+
存在距离小于等于 2 的情况,不符合距离要求
5555

5656
故而只有 arr1[0]=4 和 arr1[1]=5 两个符合距离要求,距离值为 2</pre>
5757

@@ -85,9 +85,9 @@ tags:
8585

8686
### 方法一:排序 + 二分查找
8787

88-
我们可以先对数组 $arr2$ 排序,然后对于数组 $arr1$ 中的每个元素 $a$,使用二分查找,找到数组 $arr2$ 中第一个大于等于 $a-d$ 的元素,如果元素存在,且小于等于 $a+d$,则说明不符合距离要求,否则说明符合距离要求。我们将符合距离要求的元素个数累加,即为答案。
88+
我们可以先对数组 $\textit{arr2}$ 排序,然后对于数组 $\textit{arr1}$ 中的每个元素 $x$,使用二分查找,找到数组 $\textit{arr2}$ 中第一个大于等于 $x - d$ 的元素,如果元素存在,且小于等于 $x + d$,则说明不符合距离要求,否则说明符合距离要求。我们将符合距离要求的元素个数累加,即为答案。
8989

90-
时间复杂度 $O((m + n) \times \log n)$,空间复杂度 $O(\log n)$。其中 $m$ 和 $n$ 分别是数组 $arr1$ 和 $arr2$ 的长度。
90+
时间复杂度 $O((m + n) \times \log n)$,空间复杂度 $O(\log n)$。其中 $m$ 和 $n$ 分别是数组 $\textit{arr1}$ 和 $\textit{arr2}$ 的长度。
9191

9292
<!-- tabs:start -->
9393

@@ -96,12 +96,12 @@ tags:
9696
```python
9797
class Solution:
9898
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
99-
def check(a: int) -> bool:
100-
i = bisect_left(arr2, a - d)
101-
return i == len(arr2) or arr2[i] > a + d
102-
10399
arr2.sort()
104-
return sum(check(a) for a in arr1)
100+
ans = 0
101+
for x in arr1:
102+
i = bisect_left(arr2, x - d)
103+
ans += i == len(arr2) or arr2[i] > x + d
104+
return ans
105105
```
106106

107107
#### Java
@@ -111,26 +111,15 @@ class Solution {
111111
public int findTheDistanceValue(int[] arr1, int[] arr2, int d) {
112112
Arrays.sort(arr2);
113113
int ans = 0;
114-
for (int a : arr1) {
115-
if (check(arr2, a, d)) {
114+
for (int x : arr1) {
115+
int i = Arrays.binarySearch(arr2, x - d);
116+
i = i < 0 ? -i - 1 : i;
117+
if (i == arr2.length || arr2[i] > x + d) {
116118
++ans;
117119
}
118120
}
119121
return ans;
120122
}
121-
122-
private boolean check(int[] arr, int a, int d) {
123-
int l = 0, r = arr.length;
124-
while (l < r) {
125-
int mid = (l + r) >> 1;
126-
if (arr[mid] >= a - d) {
127-
r = mid;
128-
} else {
129-
l = mid + 1;
130-
}
131-
}
132-
return l >= arr.length || arr[l] > a + d;
133-
}
134123
}
135124
```
136125

@@ -140,14 +129,13 @@ class Solution {
140129
class Solution {
141130
public:
142131
int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
143-
auto check = [&](int a) -> bool {
144-
auto it = lower_bound(arr2.begin(), arr2.end(), a - d);
145-
return it == arr2.end() || *it > a + d;
146-
};
147-
sort(arr2.begin(), arr2.end());
132+
ranges::sort(arr2);
148133
int ans = 0;
149-
for (int& a : arr1) {
150-
ans += check(a);
134+
for (int x : arr1) {
135+
auto it = ranges::lower_bound(arr2, x - d);
136+
if (it == arr2.end() || *it > x + d) {
137+
++ans;
138+
}
151139
}
152140
return ans;
153141
}
@@ -159,9 +147,9 @@ public:
159147
```go
160148
func findTheDistanceValue(arr1 []int, arr2 []int, d int) (ans int) {
161149
sort.Ints(arr2)
162-
for _, a := range arr1 {
163-
i := sort.SearchInts(arr2, a-d)
164-
if i == len(arr2) || arr2[i] > a+d {
150+
for _, x := range arr1 {
151+
i := sort.SearchInts(arr2, x-d)
152+
if i == len(arr2) || arr2[i] > x+d {
165153
ans++
166154
}
167155
}
@@ -173,23 +161,11 @@ func findTheDistanceValue(arr1 []int, arr2 []int, d int) (ans int) {
173161

174162
```ts
175163
function findTheDistanceValue(arr1: number[], arr2: number[], d: number): number {
176-
const check = (a: number) => {
177-
let l = 0;
178-
let r = arr2.length;
179-
while (l < r) {
180-
const mid = (l + r) >> 1;
181-
if (arr2[mid] >= a - d) {
182-
r = mid;
183-
} else {
184-
l = mid + 1;
185-
}
186-
}
187-
return l === arr2.length || arr2[l] > a + d;
188-
};
189164
arr2.sort((a, b) => a - b);
190-
let ans = 0;
191-
for (const a of arr1) {
192-
if (check(a)) {
165+
let ans: number = 0;
166+
for (const x of arr1) {
167+
const i = _.sortedIndex(arr2, x - d);
168+
if (i === arr2.length || arr2[i] > x + d) {
193169
++ans;
194170
}
195171
}
@@ -203,26 +179,17 @@ function findTheDistanceValue(arr1: number[], arr2: number[], d: number): number
203179
impl Solution {
204180
pub fn find_the_distance_value(arr1: Vec<i32>, mut arr2: Vec<i32>, d: i32) -> i32 {
205181
arr2.sort();
206-
let n = arr2.len();
207-
let mut res = 0;
208-
for &num in arr1.iter() {
209-
let mut left = 0;
210-
let mut right = n - 1;
211-
while left < right {
212-
let mid = left + (right - left) / 2;
213-
if arr2[mid] <= num {
214-
left = mid + 1;
215-
} else {
216-
right = mid;
217-
}
218-
}
219-
if i32::abs(num - arr2[left]) <= d || (left != 0 && i32::abs(num - arr2[left - 1]) <= d)
220-
{
221-
continue;
182+
let mut ans = 0;
183+
for &x in &arr1 {
184+
let i = match arr2.binary_search(&(x - d)) {
185+
Ok(j) => j,
186+
Err(j) => j,
187+
};
188+
if i == arr2.len() || arr2[i] > x + d {
189+
ans += 1;
222190
}
223-
res += 1;
224191
}
225-
res
192+
ans
226193
}
227194
}
228195
```

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

+43-76
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ tags:
3131
<pre>
3232
<strong>Input:</strong> arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2
3333
<strong>Output:</strong> 2
34-
<strong>Explanation:</strong>
35-
For arr1[0]=4 we have:
36-
|4-10|=6 &gt; d=2
37-
|4-9|=5 &gt; d=2
38-
|4-1|=3 &gt; d=2
39-
|4-8|=4 &gt; d=2
40-
For arr1[1]=5 we have:
41-
|5-10|=5 &gt; d=2
42-
|5-9|=4 &gt; d=2
43-
|5-1|=4 &gt; d=2
34+
<strong>Explanation:</strong>
35+
For arr1[0]=4 we have:
36+
|4-10|=6 &gt; d=2
37+
|4-9|=5 &gt; d=2
38+
|4-1|=3 &gt; d=2
39+
|4-8|=4 &gt; d=2
40+
For arr1[1]=5 we have:
41+
|5-10|=5 &gt; d=2
42+
|5-9|=4 &gt; d=2
43+
|5-1|=4 &gt; d=2
4444
|5-8|=3 &gt; d=2
4545
For arr1[2]=8 we have:
4646
<strong>|8-10|=2 &lt;= d=2</strong>
@@ -80,9 +80,9 @@ For arr1[2]=8 we have:
8080

8181
### Solution 1: Sorting + Binary Search
8282

83-
We can first sort the array $arr2$, then for each element $a$ in array $arr1$, use binary search to find the first element in array $arr2$ that is greater than or equal to $a-d$. If such an element exists and is less than or equal to $a+d$, it means that it does not meet the distance requirement. Otherwise, it meets the distance requirement. We accumulate the number of elements that meet the distance requirement, which is the answer.
83+
We can first sort the array $\textit{arr2}$, and then for each element $x$ in the array $\textit{arr1}$, use binary search to find the first element in the array $\textit{arr2}$ that is greater than or equal to $x - d$. If such an element exists and is less than or equal to $x + d$, it does not meet the distance requirement. Otherwise, it meets the distance requirement. We count the number of elements that meet the distance requirement, which is the answer.
8484

85-
The time complexity is $O((m + n) \times \log n)$, and the space complexity is $O(\log n)$. Where $m$ and $n$ are the lengths of arrays $arr1$ and $arr2$, respectively.
85+
The time complexity is $O((m + n) \times \log n)$, and the space complexity is $O(\log n)$. Here, $m$ and $n$ are the lengths of the arrays $\textit{arr1}$ and $\textit{arr2}$, respectively.
8686

8787
<!-- tabs:start -->
8888

@@ -91,12 +91,12 @@ The time complexity is $O((m + n) \times \log n)$, and the space complexity is $
9191
```python
9292
class Solution:
9393
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
94-
def check(a: int) -> bool:
95-
i = bisect_left(arr2, a - d)
96-
return i == len(arr2) or arr2[i] > a + d
97-
9894
arr2.sort()
99-
return sum(check(a) for a in arr1)
95+
ans = 0
96+
for x in arr1:
97+
i = bisect_left(arr2, x - d)
98+
ans += i == len(arr2) or arr2[i] > x + d
99+
return ans
100100
```
101101

102102
#### Java
@@ -106,26 +106,15 @@ class Solution {
106106
public int findTheDistanceValue(int[] arr1, int[] arr2, int d) {
107107
Arrays.sort(arr2);
108108
int ans = 0;
109-
for (int a : arr1) {
110-
if (check(arr2, a, d)) {
109+
for (int x : arr1) {
110+
int i = Arrays.binarySearch(arr2, x - d);
111+
i = i < 0 ? -i - 1 : i;
112+
if (i == arr2.length || arr2[i] > x + d) {
111113
++ans;
112114
}
113115
}
114116
return ans;
115117
}
116-
117-
private boolean check(int[] arr, int a, int d) {
118-
int l = 0, r = arr.length;
119-
while (l < r) {
120-
int mid = (l + r) >> 1;
121-
if (arr[mid] >= a - d) {
122-
r = mid;
123-
} else {
124-
l = mid + 1;
125-
}
126-
}
127-
return l >= arr.length || arr[l] > a + d;
128-
}
129118
}
130119
```
131120

@@ -135,14 +124,13 @@ class Solution {
135124
class Solution {
136125
public:
137126
int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
138-
auto check = [&](int a) -> bool {
139-
auto it = lower_bound(arr2.begin(), arr2.end(), a - d);
140-
return it == arr2.end() || *it > a + d;
141-
};
142-
sort(arr2.begin(), arr2.end());
127+
ranges::sort(arr2);
143128
int ans = 0;
144-
for (int& a : arr1) {
145-
ans += check(a);
129+
for (int x : arr1) {
130+
auto it = ranges::lower_bound(arr2, x - d);
131+
if (it == arr2.end() || *it > x + d) {
132+
++ans;
133+
}
146134
}
147135
return ans;
148136
}
@@ -154,9 +142,9 @@ public:
154142
```go
155143
func findTheDistanceValue(arr1 []int, arr2 []int, d int) (ans int) {
156144
sort.Ints(arr2)
157-
for _, a := range arr1 {
158-
i := sort.SearchInts(arr2, a-d)
159-
if i == len(arr2) || arr2[i] > a+d {
145+
for _, x := range arr1 {
146+
i := sort.SearchInts(arr2, x-d)
147+
if i == len(arr2) || arr2[i] > x+d {
160148
ans++
161149
}
162150
}
@@ -168,23 +156,11 @@ func findTheDistanceValue(arr1 []int, arr2 []int, d int) (ans int) {
168156

169157
```ts
170158
function findTheDistanceValue(arr1: number[], arr2: number[], d: number): number {
171-
const check = (a: number) => {
172-
let l = 0;
173-
let r = arr2.length;
174-
while (l < r) {
175-
const mid = (l + r) >> 1;
176-
if (arr2[mid] >= a - d) {
177-
r = mid;
178-
} else {
179-
l = mid + 1;
180-
}
181-
}
182-
return l === arr2.length || arr2[l] > a + d;
183-
};
184159
arr2.sort((a, b) => a - b);
185-
let ans = 0;
186-
for (const a of arr1) {
187-
if (check(a)) {
160+
let ans: number = 0;
161+
for (const x of arr1) {
162+
const i = _.sortedIndex(arr2, x - d);
163+
if (i === arr2.length || arr2[i] > x + d) {
188164
++ans;
189165
}
190166
}
@@ -198,26 +174,17 @@ function findTheDistanceValue(arr1: number[], arr2: number[], d: number): number
198174
impl Solution {
199175
pub fn find_the_distance_value(arr1: Vec<i32>, mut arr2: Vec<i32>, d: i32) -> i32 {
200176
arr2.sort();
201-
let n = arr2.len();
202-
let mut res = 0;
203-
for &num in arr1.iter() {
204-
let mut left = 0;
205-
let mut right = n - 1;
206-
while left < right {
207-
let mid = left + (right - left) / 2;
208-
if arr2[mid] <= num {
209-
left = mid + 1;
210-
} else {
211-
right = mid;
212-
}
213-
}
214-
if i32::abs(num - arr2[left]) <= d || (left != 0 && i32::abs(num - arr2[left - 1]) <= d)
215-
{
216-
continue;
177+
let mut ans = 0;
178+
for &x in &arr1 {
179+
let i = match arr2.binary_search(&(x - d)) {
180+
Ok(j) => j,
181+
Err(j) => j,
182+
};
183+
if i == arr2.len() || arr2[i] > x + d {
184+
ans += 1;
217185
}
218-
res += 1;
219186
}
220-
res
187+
ans
221188
}
222189
}
223190
```

0 commit comments

Comments
 (0)