File tree Expand file tree Collapse file tree 4 files changed +66
-4
lines changed
solution/0700-0799/0781.Rabbits in Forest Expand file tree Collapse file tree 4 files changed +66
-4
lines changed Original file line number Diff line number Diff line change 3838
3939<!-- 这里可写通用的实现逻辑 -->
4040
41+ 两只相同颜色的兔子看到的其他同色兔子数一定相同,若两只兔子看到的其它同色兔子数不同,那么这两只兔子颜色也不同。
42+
43+ 因此,将 ` answers ` 中值相同的元素分为一组,对于每一组,计算出兔子的最少数量,然后将所有组的计算结果累加,就是最终的答案。
44+
45+ 如果有 x 只兔子都回答 y,则至少有 ` ⌈x / (y + 1)⌉ ` 种不同的颜色,且每种颜色有 ` y + 1 ` 只兔子。
46+
4147<!-- tabs:start -->
4248
4349### ** Python3**
4450
4551<!-- 这里可写当前语言的特殊实现逻辑 -->
4652
4753``` python
48-
54+ class Solution :
55+ def numRabbits (self , answers : List[int ]) -> int :
56+ counter = collections.Counter()
57+ for e in answers:
58+ counter[e] += 1
59+ return sum ([math.ceil(v / (k + 1 )) * (k + 1 ) for k, v in counter.items()])
4960```
5061
5162### ** Java**
5263
5364<!-- 这里可写当前语言的特殊实现逻辑 -->
5465
5566``` java
56-
67+ class Solution {
68+ public int numRabbits (int [] answers ) {
69+ Map<Integer , Integer > counter = new HashMap<> ();
70+ for (int e : answers) {
71+ counter. put(e, counter. getOrDefault(e, 0 ) + 1 );
72+ }
73+ int res = 0 ;
74+ for (Map . Entry<Integer , Integer > entry : counter. entrySet()) {
75+ int answer = entry. getKey(), count = entry. getValue();
76+ res += (int ) Math . ceil(count / ((answer + 1 ) * 1.0 )) * (answer + 1 );
77+ }
78+ return res;
79+ }
80+ }
5781```
5882
5983### ** ...**
Original file line number Diff line number Diff line change @@ -56,13 +56,31 @@ The smallest possible number of rabbits in the forest is therefore 5: 3 that ans
5656### ** Python3**
5757
5858``` python
59-
59+ class Solution :
60+ def numRabbits (self , answers : List[int ]) -> int :
61+ counter = collections.Counter()
62+ for e in answers:
63+ counter[e] += 1
64+ return sum ([math.ceil(v / (k + 1 )) * (k + 1 ) for k, v in counter.items()])
6065```
6166
6267### ** Java**
6368
6469``` java
65-
70+ class Solution {
71+ public int numRabbits (int [] answers ) {
72+ Map<Integer , Integer > counter = new HashMap<> ();
73+ for (int e : answers) {
74+ counter. put(e, counter. getOrDefault(e, 0 ) + 1 );
75+ }
76+ int res = 0 ;
77+ for (Map . Entry<Integer , Integer > entry : counter. entrySet()) {
78+ int answer = entry. getKey(), count = entry. getValue();
79+ res += (int ) Math . ceil(count / ((answer + 1 ) * 1.0 )) * (answer + 1 );
80+ }
81+ return res;
82+ }
83+ }
6684```
6785
6886### ** ...**
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int numRabbits (int [] answers ) {
3+ Map <Integer , Integer > counter = new HashMap <>();
4+ for (int e : answers ) {
5+ counter .put (e , counter .getOrDefault (e , 0 ) + 1 );
6+ }
7+ int res = 0 ;
8+ for (Map .Entry <Integer , Integer > entry : counter .entrySet ()) {
9+ int answer = entry .getKey (), count = entry .getValue ();
10+ res += (int ) Math .ceil (count / ((answer + 1 ) * 1.0 )) * (answer + 1 );
11+ }
12+ return res ;
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def numRabbits (self , answers : List [int ]) -> int :
3+ counter = collections .Counter ()
4+ for e in answers :
5+ counter [e ] += 1
6+ return sum ([math .ceil (v / (k + 1 )) * (k + 1 ) for k , v in counter .items ()])
You can’t perform that action at this time.
0 commit comments