You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/1300-1399/1385.Find the Distance Value Between Two Arrays/README_EN.md
+43-76
Original file line number
Diff line number
Diff line change
@@ -31,16 +31,16 @@ tags:
31
31
<pre>
32
32
<strong>Input:</strong> arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2
33
33
<strong>Output:</strong> 2
34
-
<strong>Explanation:</strong>
35
-
For arr1[0]=4 we have:
36
-
|4-10|=6 > d=2
37
-
|4-9|=5 > d=2
38
-
|4-1|=3 > d=2
39
-
|4-8|=4 > d=2
40
-
For arr1[1]=5 we have:
41
-
|5-10|=5 > d=2
42
-
|5-9|=4 > d=2
43
-
|5-1|=4 > d=2
34
+
<strong>Explanation:</strong>
35
+
For arr1[0]=4 we have:
36
+
|4-10|=6 > d=2
37
+
|4-9|=5 > d=2
38
+
|4-1|=3 > d=2
39
+
|4-8|=4 > d=2
40
+
For arr1[1]=5 we have:
41
+
|5-10|=5 > d=2
42
+
|5-9|=4 > d=2
43
+
|5-1|=4 > d=2
44
44
|5-8|=3 > d=2
45
45
For arr1[2]=8 we have:
46
46
<strong>|8-10|=2 <= d=2</strong>
@@ -80,9 +80,9 @@ For arr1[2]=8 we have:
80
80
81
81
### Solution 1: Sorting + Binary Search
82
82
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.
84
84
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.
86
86
87
87
<!-- tabs:start -->
88
88
@@ -91,12 +91,12 @@ The time complexity is $O((m + n) \times \log n)$, and the space complexity is $
0 commit comments