Skip to content
LC-0781 Medium LeetCode

781. Rabbits in Forest

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 58% Topics: Array, Hash Table, Math, Greedy
View full problem on LeetCode
Reference solution (spoiler · python)
# 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.iteritems())

Solution from kamyu104/LeetCode-Solutions · MIT