Skip to content

Commit ada70a6

Browse files
committed
feat: add solutions to lc problem: No.1385. Find the Distance Value Between Two Arrays
1 parent 7027ec7 commit ada70a6

File tree

6 files changed

+211
-4
lines changed

6 files changed

+211
-4
lines changed

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

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,99 @@
6262
<li><code>0 &lt;= d &lt;= 100</code></li>
6363
</ul>
6464

65-
6665
## 解法
6766

6867
<!-- 这里可写通用的实现逻辑 -->
6968

69+
由于 arr1,arr2 的长度不超过 500,直接暴力遍历即可。
70+
7071
<!-- tabs:start -->
7172

7273
### **Python3**
7374

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

7677
```python
77-
78+
class Solution:
79+
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
80+
res = 0
81+
for a in arr1:
82+
exist = False
83+
for b in arr2:
84+
if abs(a - b) <= d:
85+
exist = True
86+
break
87+
if not exist:
88+
res += 1
89+
return res
7890
```
7991

8092
### **Java**
8193

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

8496
```java
97+
class Solution {
98+
public int findTheDistanceValue(int[] arr1, int[] arr2, int d) {
99+
int res = 0;
100+
for (int a : arr1) {
101+
boolean exist = false;
102+
for (int b : arr2) {
103+
if (Math.abs(a - b) <= d) {
104+
exist = true;
105+
break;
106+
}
107+
}
108+
if (!exist) {
109+
++res;
110+
}
111+
}
112+
return res;
113+
}
114+
}
115+
```
116+
117+
### **C++**
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
123+
int res = 0;
124+
for (auto& a : arr1) {
125+
bool exist = false;
126+
for (auto& b : arr2) {
127+
if (abs(a - b) <= d) {
128+
exist = true;
129+
break;
130+
}
131+
}
132+
if (!exist) ++res;
133+
}
134+
return res;
135+
}
136+
};
137+
```
85138
139+
### **Go**
140+
141+
```go
142+
func findTheDistanceValue(arr1 []int, arr2 []int, d int) int {
143+
res := 0
144+
for _, a := range arr1 {
145+
exist := false
146+
for _, b := range arr2 {
147+
if math.Abs(float64(a-b)) <= float64(d) {
148+
exist = true
149+
break
150+
}
151+
}
152+
if !exist {
153+
res++
154+
}
155+
}
156+
return res
157+
}
86158
```
87159

88160
### **...**

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

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,93 @@ For arr1[2]=8 we have:
5555
<li><code>0 &lt;= d &lt;= 100</code></li>
5656
</ul>
5757

58-
5958
## Solutions
6059

60+
Brute-force.
61+
6162
<!-- tabs:start -->
6263

6364
### **Python3**
6465

6566
```python
66-
67+
class Solution:
68+
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
69+
res = 0
70+
for a in arr1:
71+
exist = False
72+
for b in arr2:
73+
if abs(a - b) <= d:
74+
exist = True
75+
break
76+
if not exist:
77+
res += 1
78+
return res
6779
```
6880

6981
### **Java**
7082

7183
```java
84+
class Solution {
85+
public int findTheDistanceValue(int[] arr1, int[] arr2, int d) {
86+
int res = 0;
87+
for (int a : arr1) {
88+
boolean exist = false;
89+
for (int b : arr2) {
90+
if (Math.abs(a - b) <= d) {
91+
exist = true;
92+
break;
93+
}
94+
}
95+
if (!exist) {
96+
++res;
97+
}
98+
}
99+
return res;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
110+
int res = 0;
111+
for (auto& a : arr1) {
112+
bool exist = false;
113+
for (auto& b : arr2) {
114+
if (abs(a - b) <= d) {
115+
exist = true;
116+
break;
117+
}
118+
}
119+
if (!exist) ++res;
120+
}
121+
return res;
122+
}
123+
};
124+
```
72125
126+
### **Go**
127+
128+
```go
129+
func findTheDistanceValue(arr1 []int, arr2 []int, d int) int {
130+
res := 0
131+
for _, a := range arr1 {
132+
exist := false
133+
for _, b := range arr2 {
134+
if math.Abs(float64(a-b)) <= float64(d) {
135+
exist = true
136+
break
137+
}
138+
}
139+
if !exist {
140+
res++
141+
}
142+
}
143+
return res
144+
}
73145
```
74146

75147
### **...**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
4+
int res = 0;
5+
for (auto& a : arr1) {
6+
bool exist = false;
7+
for (auto& b : arr2) {
8+
if (abs(a - b) <= d) {
9+
exist = true;
10+
break;
11+
}
12+
}
13+
if (!exist) ++res;
14+
}
15+
return res;
16+
}
17+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func findTheDistanceValue(arr1 []int, arr2 []int, d int) int {
2+
res := 0
3+
for _, a := range arr1 {
4+
exist := false
5+
for _, b := range arr2 {
6+
if math.Abs(float64(a-b)) <= float64(d) {
7+
exist = true
8+
break
9+
}
10+
}
11+
if !exist {
12+
res++
13+
}
14+
}
15+
return res
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int findTheDistanceValue(int[] arr1, int[] arr2, int d) {
3+
int res = 0;
4+
for (int a : arr1) {
5+
boolean exist = false;
6+
for (int b : arr2) {
7+
if (Math.abs(a - b) <= d) {
8+
exist = true;
9+
break;
10+
}
11+
}
12+
if (!exist) {
13+
++res;
14+
}
15+
}
16+
return res;
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
3+
res = 0
4+
for a in arr1:
5+
exist = False
6+
for b in arr2:
7+
if abs(a - b) <= d:
8+
exist = True
9+
break
10+
if not exist:
11+
res += 1
12+
return res

0 commit comments

Comments
 (0)