8000 add 2670 · srivtx/Leetcode@588232b · GitHub
[go: up one dir, main page]

Skip to content

Commit 588232b

Browse files
add 2670
1 parent 93499bc commit 588232b

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-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+
| 2670 |[Find the Distinct Difference Array](https://leetcode.com/problems/find-the-distinct-difference-array/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2670.java) | | Easy |
1112
| 2596 |[Check Knight Tour Configuration](https://leetcode.com/problems/check-knight-tour-configuration/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2596.java) | [:tv:](https://youtu.be/OBht8NT_09c) | Medium |
1213
| 2595 |[Number of Even and Odd Bits](https://leetcode.com/problems/number-of-even-and-odd-bits/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2595.java) | | Easy |
1314
| 2586 |[Count the Number of Vowel Strings in Range](https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range/)| [Java](../master/src/main/java/com/fishercoder/solutions/_2586.java) | | Easy |
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _2670 {
7+
public static class Solution1 {
8+
public int[] distinctDifferenceArray(int[] nums) {
9+
Map<Integer, Integer> prefix = new HashMap<>();
10+
prefix.put(nums[0], 1);
11+
Map<Integer, Integer> suffix = new HashMap<>();
12+
for (int i = 1; i < nums.length; i++) {
13+
suffix.put(nums[i], suffix.getOrDefault(nums[i], 0) + 1);
14+
}
15+
int[] result = new int[nums.length];
16+
for (int i = 0; i < nums.length; i++) {
17+
result[i] = prefix.size() - suffix.size();
18+
if (i + 1 < nums.length) {
19+
prefix.put(nums[i + 1], prefix.getOrDefault(nums[i + 1], 0) + 1);
20+
if (suffix.containsKey(nums[i + 1]) && suffix.get(nums[i + 1]) == 1) {
21+
suffix.remove(nums[i + 1]);
22+
} else {
23+
suffix.put(nums[i + 1], suffix.getOrDefault(nums[i + 1], 0) - 1);
24+
}
25+
}
26+
}
27+
return result;
28+
}
29+
}
30+
}
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._2670;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _2670Test {
10+
private static _2670.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _2670.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new int[]{-2, -1, 0, 2, 3}, solution1.distinctDifferenceArray(new int[]{3, 2, 3, 4, 2}));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)
0