Skip to content

Commit f3ae25c

Browse files
add 1385
1 parent ad97617 commit f3ae25c

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1385|[Find the Distance Value Between Two Arrays](https://leetcode.com/problems/find-the-distance-value-between-two-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1385.java) | |Easy|Array|
1112
|1382|[Balance a Binary Search Tree](https://leetcode.com/problems/balance-a-binary-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1382.java) | |Medium|Binary Search Tree|
1213
|1381|[Design a Stack With Increment Operation](https://leetcode.com/problems/design-a-stack-with-increment-operation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1381.java) | |Medium|Stack, Design|
1314
|1380|[Lucky Numbers in a Matrix](https://leetcode.com/problems/lucky-numbers-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1380.java) | |Easy|Array|
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1385. Find the Distance Value Between Two Arrays
5+
*
6+
* Given two integer arrays arr1 and arr2, and the integer d, return the distance value between the two arrays.
7+
* The distance value is defined as the number of elements arr1[i] such that there is not any element arr2[j] where |arr1[i]-arr2[j]| <= d.
8+
*
9+
* Example 1:
10+
* Input: arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2
11+
* Output: 2
12+
* Explanation:
13+
* For arr1[0]=4 we have:
14+
* |4-10|=6 > d=2
15+
* |4-9|=5 > d=2
16+
* |4-1|=3 > d=2
17+
* |4-8|=4 > d=2
18+
* For arr1[1]=5 we have:
19+
* |5-10|=5 > d=2
20+
* |5-9|=4 > d=2
21+
* |5-1|=4 > d=2
22+
* |5-8|=3 > d=2
23+
* For arr1[2]=8 we have:
24+
* |8-10|=2 <= d=2
25+
* |8-9|=1 <= d=2
26+
* |8-1|=7 > d=2
27+
* |8-8|=0 <= d=2
28+
*
29+
* Example 2:
30+
* Input: arr1 = [1,4,2,3], arr2 = [-4,-3,6,10,20,30], d = 3
31+
* Output: 2
32+
*
33+
* Example 3:
34+
* Input: arr1 = [2,1,100,3], arr2 = [-5,-2,10,-3,7], d = 6
35+
* Output: 1
36+
*
37+
* Constraints:
38+
* 1 <= arr1.length, arr2.length <= 500
39+
* -10^3 <= arr1[i], arr2[j] <= 10^3
40+
* 0 <= d <= 100
41+
* */
42+
public class _1385 {
43+
public static class Solution1 {
44+
public int findTheDistanceValue(int[] arr1, int[] arr2, int d) {
45+
int count = 0;
46+
for (int i = 0; i < arr1.length; i++) {
47+
int j = 0;
48+
for (; j < arr2.length; j++) {
49+
if (Math.abs(arr1[i] - arr2[j]) <= d) {
50+
break;
51+
}
52+
}
53+
if (j == arr2.length) {
54+
count++;
55+
}
56+
}
57+
return count;
58+
}
59+
}
60+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1385;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1385Test {
10+
private static _1385.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1385.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(2, solution1.findTheDistanceValue(new int[]{4, 5, 8}, new int[]{10, 9, 1, 8}, 2));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)