forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_781.java
31 lines (29 loc) · 940 Bytes
/
_781.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
public class _781 {
public static class Solution1 {
public int numRabbits(int[] answers) {
Map<Integer, Integer> map = new HashMap<>();
int rabbits = 0;
for (int rabbitType : answers) {
if (map.containsKey(rabbitType)) {
int count = map.get(rabbitType);
count--;
if (count == 0) {
map.remove(rabbitType);
} else {
map.put(rabbitType, count);
}
} else {
rabbits += rabbitType;
rabbits++;
if (rabbitType != 0) {
map.put(rabbitType, rabbitType);
}
}
}
return rabbits;
}
}
}