Skip to content

Commit

Permalink
feat: add solutions to leetcode problem: No. 0781. Rabbits in Forest
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Apr 4, 2021
1 parent 2113330 commit 89cdf31
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
28 changes: 26 additions & 2 deletions solution/0700-0799/0781.Rabbits in Forest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,46 @@

<!-- 这里可写通用的实现逻辑 -->

两只相同颜色的兔子看到的其他同色兔子数一定相同,若两只兔子看到的其它同色兔子数不同,那么这两只兔子颜色也不同。

因此,将 `answers` 中值相同的元素分为一组,对于每一组,计算出兔子的最少数量,然后将所有组的计算结果累加,就是最终的答案。

如果有 x 只兔子都回答 y,则至少有 `⌈x / (y + 1)⌉` 种不同的颜色,且每种颜色有 `y + 1` 只兔子。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def numRabbits(self, answers: List[int]) -> int:
counter = collections.Counter()
for e in answers:
counter[e] += 1
return sum([math.ceil(v / (k + 1)) * (k + 1) for k, v in counter.items()])
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

class Solution {
public int numRabbits(int[] answers) {
Map<Integer, Integer> counter = new HashMap<>();
for (int e : answers) {
counter.put(e, counter.getOrDefault(e, 0) + 1);
}
int res = 0;
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
int answer = entry.getKey(), count = entry.getValue();
res += (int) Math.ceil(count / ((answer + 1) * 1.0)) * (answer + 1);
}
return res;
}
}
```

### **...**
Expand Down
22 changes: 20 additions & 2 deletions solution/0700-0799/0781.Rabbits in Forest/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,31 @@ The smallest possible number of rabbits in the forest is therefore 5: 3 that ans
### **Python3**

```python

class Solution:
def numRabbits(self, answers: List[int]) -> int:
counter = collections.Counter()
for e in answers:
counter[e] += 1
return sum([math.ceil(v / (k + 1)) * (k + 1) for k, v in counter.items()])
```

### **Java**

```java

class Solution {
public int numRabbits(int[] answers) {
Map<Integer, Integer> counter = new HashMap<>();
for (int e : answers) {
counter.put(e, counter.getOrDefault(e, 0) + 1);
}
int res = 0;
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
int answer = entry.getKey(), count = entry.getValue();
res += (int) Math.ceil(count / ((answer + 1) * 1.0)) * (answer + 1);
}
return res;
}
}
```

### **...**
Expand Down
14 changes: 14 additions & 0 deletions solution/0700-0799/0781.Rabbits in Forest/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public int numRabbits(int[] answers) {
Map<Integer, Integer> counter = new HashMap<>();
for (int e : answers) {
counter.put(e, counter.getOrDefault(e, 0) + 1);
}
int res = 0;
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
int answer = entry.getKey(), count = entry.getValue();
res += (int) Math.ceil(count / ((answer + 1) * 1.0)) * (answer + 1);
}
return res;
}
}
6 changes: 6 additions & 0 deletions solution/0700-0799/0781.Rabbits in Forest/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution:
def numRabbits(self, answers: List[int]) -> int:
counter = collections.Counter()
for e in answers:
counter[e] += 1
return sum([math.ceil(v / (k + 1)) * (k + 1) for k, v in counter.items()])

0 comments on commit 89cdf31

Please sign in to comment.