781. Rabbits in Forest Medium

@problem@discussion
#Array#Hash Table#Math#Greedy



1/**
2 * [781] Rabbits in Forest
3 *
4 * There is a forest with an unknown number of rabbits. We asked n rabbits "How many rabbits have the same color as you?" and collected the answers in an integer array answers where answers[i] is the answer of the i^th rabbit.
5 * Given the array answers, return the minimum number of rabbits that could be in the forest.
6 *  
7 * Example 1:
8 * 
9 * Input: answers = [1,1,2]
10 * Output: 5
11 * Explanation:
12 * The two rabbits that answered "1" could both be the same color, say red.
13 * The rabbit that answered "2" can't be red or the answers would be inconsistent.
14 * Say the rabbit that answered "2" was blue.
15 * Then there should be 2 other blue rabbits in the forest that didn't answer into the array.
16 * The smallest possible number of rabbits in the forest is therefore 5: 3 that answered plus 2 that didn't.
17 * 
18 * Example 2:
19 * 
20 * Input: answers = [10,10,10]
21 * Output: 11
22 * 
23 *  
24 * Constraints:
25 * 
26 * 	1 <= answers.length <= 1000
27 * 	0 <= answers[i] < 1000
28 * 
29 */
30pub struct Solution {}
31
32// problem: https://leetcode.com/problems/rabbits-in-forest/
33// discuss: https://leetcode.com/problems/rabbits-in-forest/discuss/?currentPage=1&orderBy=most_votes&query=
34
35// submission codes start here
36
37impl Solution {
38    pub fn num_rabbits(answers: Vec<i32>) -> i32 {
39        0
40    }
41}
42
43// submission codes end
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_781() {
51    }
52}
53


Back
© 2025 bowen.ge All Rights Reserved.