Skip to content

Commit 459215b

Browse files
add 781
1 parent e906334 commit 459215b

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ _If you like this project, please leave me a star._ ★
362362
|788|[Rotated Digits](https://leetcode.com/problems/rotated-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_788.java) | |Easy|
363363
|784|[Letter Case Permutation](https://leetcode.com/problems/letter-case-permutation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_784.java) | |Easy|
364364
|783|[Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_783.java) | |Easy|
365+
|781|[Rabbits in Forest](https://leetcode.com/problems/rabbits-in-forest/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_781.java) | |Medium| HashTable, Math
365366
|779|[K-th Symbol in Grammar](https://leetcode.com/problems/k-th-symbol-in-grammar/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_779.java) | |Medium|
366367
|776|[Split BST](https://leetcode.com/problems/split-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_776.java) | |Medium| Recursion
367368
|771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_771.java) | |Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _781 {
7+
public static class Solution1 {
8+
public int numRabbits(int[] answers) {
9+
Map<Integer, Integer> map = new HashMap<>();
10+
int rabbits = 0;
11+
for (int rabbitType : answers) {
12+
if (map.containsKey(rabbitType)) {
13+
int count = map.get(rabbitType);
14+
count--;
15+
if (count == 0) {
16+
map.remove(rabbitType);
17+
} else {
18+
map.put(rabbitType, count);
19+
}
20+
} else {
21+
rabbits += rabbitType;
22+
rabbits++;
23+
if (rabbitType != 0) {
24+
map.put(rabbitType, rabbitType);
25+
}
26+
}
27+
}
28+
return rabbits;
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._781;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _781Test {
10+
private static _781.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _781.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(5, solution1.numRabbits(new int[]{1, 1, 2}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(11, solution1.numRabbits(new int[]{10, 10, 10}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(0, solution1.numRabbits(new int[]{}));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(5, solution1.numRabbits(new int[]{1, 0, 1, 0, 0}));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(7, solution1.numRabbits(new int[]{1, 1, 1, 2, 2, 2}));
40+
}
41+
42+
@Test
43+
public void test6() {
44+
assertEquals(13, solution1.numRabbits(new int[]{2, 1, 2, 2, 2, 2, 2, 2, 1, 1}));
45+
}
46+
}

0 commit comments

Comments
 (0)