Leetcode 797 Solution

This article provides solution to leetcode question 797 (rabbits-in-forest)

https://leetcode.com/problems/rabbits-in-forest

Solution

class Solution:
    def numRabbits(self, answers: List[int]) -> int:
        m = collections.defaultdict(int)
        for a in answers:
            m[a] += 1

        ans = 0
        for k, v in m.items():
            if k == 0:
                ans += v
            else:
                ans += ((v + k) // (k + 1)) * (k + 1)
        return ans