-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
781.java
46 lines (45 loc) · 1.31 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
__________________________________________________________________________________________________
sample 0 ms submission
class Solution {
public int numRabbits(int[] answers) {
if (answers == null || answers.length == 0) {
return 0;
}
int[] cnt = new int[1000];
int res = 0;
for (int i: answers) {
if (cnt[i] > 0) {
cnt[i]--;
} else {
res += i + 1;
cnt[i] = i;
}
}
return res;
}
}
__________________________________________________________________________________________________
sample 36732 kb submission
class Solution {
public int numRabbits(int[] answers) {
HashMap<Integer, Integer> map=new HashMap<Integer, Integer>();
int count=0;
for(int i: answers){
if(i==0){
count++;
}
else if(map.containsKey(i)){
map.put(i,map.get(i)-1);
if(map.get(i)==0){
map.remove(i);
}
}
else{
map.put(i,i);
count+=(i+1);
}
}
return count;
}
}
__________________________________________________________________________________________________