LeetCode781(2021.4.4)

LC781. 森林中的兔子

class Solution {
    public int numRabbits(int[] answers) {
        Map<Integer, Integer> mp = new HashMap<Integer, Integer>();
        for (int y : answers) {
            mp.put(y, mp.getOrDefault(y, 0) + 1);
        }
        int res = 0;
        for (Map.Entry<Integer, Integer> item : mp.entrySet()) {
            int y = item.getKey(), x = item.getValue();
            res += (x + y) / (y + 1) * (y + 1);
        }
        return res;
    }
}

留下评论