跳转至

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

 

提示:

  • 1 <= answers.length <= 1000
  • 0 <= answers[i] < 1000

解法

方法一:贪心 + 哈希表

根据题目描述,回答相同的兔子,可能属于同一种颜色,而回答不同的兔子,不可能属于同一种颜色。

因此,我们用一个哈希表 \(\textit{cnt}\) 记录每种回答出现的次数。对于每种回答 \(x\) 及其出现次数 \(v\),我们按照每种颜色有 \(x + 1\) 只兔子的原则,计算出兔子的最少数量,并将其加入答案。

时间复杂度 \(O(n)\),空间复杂度 \(O(n)\)。其中 \(n\) 为数组 \(\textit{answers}\) 的长度。

1
2
3
4
5
6
7
8
class Solution:
    def numRabbits(self, answers: List[int]) -> int:
        cnt = Counter(answers)
        ans = 0
        for x, v in cnt.items():
            group = x + 1
            ans += (v + group - 1) // group * group
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    public int numRabbits(int[] answers) {
        Map<Integer, Integer> cnt = new HashMap<>();
        for (int x : answers) {
            cnt.merge(x, 1, Integer::sum);
        }
        int ans = 0;
        for (var e : cnt.entrySet()) {
            int group = e.getKey() + 1;
            ans += (e.getValue() + group - 1) / group * group;
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
public:
    int numRabbits(vector<int>& answers) {
        unordered_map<int, int> cnt;
        for (int x : answers) {
            ++cnt[x];
        }
        int ans = 0;
        for (auto& [x, v] : cnt) {
            int group = x + 1;
            ans += (v + group - 1) / group * group;
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
func numRabbits(answers []int) (ans int) {
    cnt := map[int]int{}
    for _, x := range answers {
        cnt[x]++
    }
    for x, v := range cnt {
        group := x + 1
        ans += (v + group - 1) / group * group
    }
    return
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
function numRabbits(answers: number[]): number {
    const cnt = new Map<number, number>();
    for (const x of answers) {
        cnt.set(x, (cnt.get(x) || 0) + 1);
    }
    let ans = 0;
    for (const [x, v] of cnt.entries()) {
        const group = x + 1;
        ans += Math.floor((v + group - 1) / group) * group;
    }
    return ans;
}

Solution 2: Greedy + Hash Map

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
function numRabbits(answers: number[]): number {
    const cnt: Record<number, number> = {};
    let ans = 0;

    for (const x of answers) {
        if (cnt[x]) {
            cnt[x]--;
        } else {
            cnt[x] = x;
            ans += x + 1;
        }
    }

    return ans;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
function numRabbits(answers) {
    const cnt = {};
    let ans = 0;

    for (const x of answers) {
        if (cnt[x]) {
            cnt[x]--;
        } else {
            cnt[x] = x;
            ans += x + 1;
        }
    }

    return ans;
}

评论

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy