-
Notifications
You must be signed in to change notification settings - Fork 43
/
rabbits-in-forest.py
46 lines (43 loc) · 1.15 KB
/
rabbits-in-forest.py
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
# V1 : dev
# V2
# https://blog.csdn.net/fuxuemingzhu/article/details/79457764
import collections
class Solution(object):
def numRabbits(self, answers):
"""
:type answers: List[int]
:rtype: int
"""
count = collections.Counter(answers)
print (count)
return sum((count[x] + x) / (x + 1) * (x + 1) for x in count)
# V3
# http://bookshadow.com/weblog/2018/02/16/leetcode-rabbits-in-forest/
#### Greedy Algorithm ###
class Solution(object):
def numRabbits(self, answers):
"""
:type answers: List[int]
:rtype: int
"""
ans = 0
cntDict = collections.defaultdict(int)
for n in answers:
if cntDict[n + 1]:
cntDict[n + 1] -= 1
else:
ans += n + 1
cntDict[n + 1] = n
return ans
# V4
# Time: O(n)
# Space: O(n)
import collections
class Solution(object):
def numRabbits(self, answers):
"""
:type answers: List[int]
:rtype: int
"""
count = collections.Counter(answers)
return sum((((k+1)+v-1)//(k+1))*(k+1) for k, v in count.items())