列表

详情


781. 森林中的兔子

森林中有未知数量的兔子。提问其中若干只兔子 "还有多少只兔子与你(指被提问的兔子)颜色相同?" ,将答案收集到一个整数数组 answers 中,其中 answers[i] 是第 i 只兔子的回答。

给你数组 answers ,返回森林中兔子的最少数量。

 

示例 1:

输入:answers = [1,1,2]
输出:5
解释:
两只回答了 "1" 的兔子可能有相同的颜色,设为红色。 
之后回答了 "2" 的兔子不会是红色,否则他们的回答会相互矛盾。
设回答了 "2" 的兔子为蓝色。 
此外,森林中还应有另外 2 只蓝色兔子的回答没有包含在数组中。 
因此森林中兔子的最少数量是 5 只:3 只回答的和 2 只没有回答的。

示例 2:

输入:answers = [10,10,10]
输出:11

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: int numRabbits(vector<int>& answers) { } };

golang 解法, 执行用时: 1 ms, 内存消耗: 4.5 MB, 提交时间: 2025-04-20 09:53:12

func numRabbits(answers []int) (ans int) {
    left := map[int]int{}
    for _, x := range answers {
        if left[x] == 0 {
            ans += x + 1 // 找到了一个大小为 x+1 的颜色组
            left[x] = x  // 允许其他 x 只兔子也回答 x
        } else {
            left[x]--
        }
    }
    return
}

javascript 解法, 执行用时: 0 ms, 内存消耗: 54.6 MB, 提交时间: 2025-04-20 09:52:39

/**
 * @param {number[]} answers
 * @return {number}
 */
var numRabbits = function(answers) {
    let ans = 0;
    const left = new Map();
    for (const x of answers) {
        const c = left.get(x) ?? 0;
        if (c == 0) {
            ans += x + 1; // 找到了一个大小为 x+1 的颜色组
            left.set(x, x); // 允许其他 x 只兔子也回答 x
        } else {
            left.set(x, c - 1);
        }
    }
    return ans;
};

rust 解法, 执行用时: 0 ms, 内存消耗: 2.3 MB, 提交时间: 2025-04-20 09:52:21

use std::collections::HashMap;

impl Solution {
    pub fn num_rabbits(answers: Vec<i32>) -> i32 {
        let mut ans = 0;
        let mut left = HashMap::new();
        for x in answers {
            let c = left.entry(x).or_insert(0);
            if *c == 0 {
                ans += x + 1; // 找到了一个大小为 x+1 的颜色组
                *c = x; // 允许其他 x 只兔子也回答 x
            } else {
                *c -= 1;
            }
        }
        ans
    }
}

python3 解法, 执行用时: 52 ms, 内存消耗: 15 MB, 提交时间: 2022-12-04 13:35:00

class Solution:
    def numRabbits(self, answers: List[int]) -> int:
        answers.sort()
        i, ans, n = 0, 0, len(answers)
        while i < n:
            cur = answers[i]
            ans += (cur + 1)
            while cur > 0 and i + 1 < len(answers) and answers[i] == answers[i+1]:
                cur -= 1
                i += 1
            i += 1
        return ans

java 解法, 执行用时: 3 ms, 内存消耗: 40.9 MB, 提交时间: 2022-12-04 13:34:16

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

golang 解法, 执行用时: 0 ms, 内存消耗: 2.7 MB, 提交时间: 2022-12-04 13:33:57

func numRabbits(answers []int) (ans int) {
    count := map[int]int{}
    for _, y := range answers {
        count[y]++
    }
    for y, x := range count {
        ans += (x + y) / (y + 1) * (y + 1)
    }
    return
}

javascript 解法, 执行用时: 68 ms, 内存消耗: 43.4 MB, 提交时间: 2022-12-04 13:33:41

/**
 * @param {number[]} answers
 * @return {number}
 */
var numRabbits = function(answers) {
    const count = new Map();
    for (const y of answers) {
        count.set(y, (count.get(y) || 0) + 1);
    }
    let ans = 0;
    for (const [y, x] of count.entries()) {
        ans += Math.floor((x + y) / (y + 1)) * (y + 1);
    }
    return ans;
};

python3 解法, 执行用时: 40 ms, 内存消耗: 15.1 MB, 提交时间: 2022-12-04 13:33:19

class Solution:
    def numRabbits(self, answers: List[int]) -> int:
        c = Counter(answers)
        ans = 0
        for k, v in c.items():
            ans += (v + k) // (k + 1) * (k + 1)
        return ans

上一题