📜  必须存在于森林中的最小兔子数量

📅  最后修改于: 2021-10-27 16:49:56             🧑  作者: Mango

森林里有一些彩色的兔子。给定的大小为N的阵列ARR [],使得具有ARR [i]表示兔子的数目相同的颜色的i兔子,任务是找到兔子,可能是在森林的最小数量。

例子:

方法:解决这个问题的方法是找出颜色相同的兔子的组数和每组兔子数量。以下是步骤:

  • 初始化一个变量count来存储每组兔子的数量。
  • 初始化映射并遍历数组,其键为arr[i] ,值为arr[i]在给定数组中的出现次数
  • 现在,如果y只兔子回答x ,则:
    • 如果(y%(x + 1))0 ,则必须有(y / (x + 1))(x + 1)只兔子。
    • 如果(y % (x + 1))非零,则必须有(y / (x + 1)) + 1(x + 1)兔子。
  • 将组数和每组兔子数的乘积添加到变量count 中
  • 经过上述步骤后, count的值给出了森林中兔子的最小数量。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum
// number of rabbits in the forest
int minNumberOfRabbits(int answers[], int N)
{
 
    // Initialize map
    map Map;
 
    // Traverse array and map arr[i]
    // to the number of occurences
    for (int a = 0; a < N; a++) {
        Map[answers[a]]++;
    }
 
    // Initialize count as 0;
    int count = 0;
 
    // Find the number groups and
    // no. of rabbits in each group
    for (auto a : Map) {
        int x = a.first;
        int y = a.second;
 
        // Find number of groups and
        // multiply them with number
        // of rabbits in each group
        if (y % (x + 1) == 0)
            count = count + (y / (x + 1)) * (x + 1);
        else
            count = count + ((y / (x + 1)) + 1) * (x + 1);
    }
 
    // count gives minimum number
    // of rabbits in the forest
    return count;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 2, 0 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << minNumberOfRabbits(arr, N) << endl;
 
    return 0;
}
 
// This code is contributed by divyeshrabadiya07


Java
// Java program for the above approach
 
import java.util.*;
 
class GFG {
 
    // Function to find the minimum
    // number of rabbits in the forest
    public static int minNumberOfRabbits(int[] answers,
                                         int N)
    {
        // Initialize map
        Map map
            = new HashMap();
 
        // Traverse array and map arr[i]
        // to the number of occurences
        for (int a : answers) {
            map.put(a, map.getOrDefault(a, 0) + 1);
        }
 
        // Initialize count as 0;
        int count = 0;
 
        // Find the number groups and
        // no. of rabbits in each group
        for (int a : map.keySet()) {
            int x = a;
            int y = map.get(a);
 
            // Find number of groups and
            // multiply them with number
            // of rabbits in each group
            if (y % (x + 1) == 0) {
                count = count + (y / (x + 1)) * (x + 1);
            }
            else
                count
                    = count + ((y / (x + 1)) + 1) * (x + 1);
        }
 
        // count gives minimum number
        // of rabbits in the forest
        return count;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 2, 2, 0 };
        int N = arr.length;
 
        // Function Call
        System.out.println(minNumberOfRabbits(arr, N));
    }
}


Python3
# Python3 program for the above approach
 
# Function to find the minimum
# number of rabbits in the forest
 
 
def minNumberOfRabbits(answers, N):
 
    # Initialize map
    Map = {}
 
    # Traverse array and map arr[i]
    # to the number of occurences
    for a in range(N):
 
        if answers[a] in Map:
            Map[answers[a]] += 1
        else:
            Map[answers[a]] = 1
 
    # Initialize count as 0;
    count = 0
 
    # Find the number groups and
    # no. of rabbits in each group
    for a in Map:
 
        x = a
        y = Map[a]
 
        # Find number of groups and
        # multiply them with number
        # of rabbits in each group
        if (y % (x + 1) == 0):
            count = count + (y // (x + 1)) * (x + 1)
        else:
            count = count + ((y // (x + 1)) + 1) * (x + 1)
 
    # count gives minimum number
    # of rabbits in the forest
    return count
 
 
# Driver code
arr = [2, 2, 0]
N = len(arr)
 
# Function Call
print(minNumberOfRabbits(arr, N))
 
# This code is contributed by divyesh072019


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
 
class GFG {
 
    // Function to find the minimum
    // number of rabbits in the forest
    public static int minNumberOfRabbits(int[] answers,
                                         int N)
    {
 
        // Initialize map
        Dictionary map
            = new Dictionary();
 
        // Traverse array and map arr[i]
        // to the number of occurences
        for (int a = 0; a < N; a++) {
            if (map.ContainsKey(answers[a]))
                map[answers[a]] += 1;
            else
                map.Add(answers[a], 1);
        }
 
        // Initialize count as 0;
        int count = 0;
 
        // Find the number groups and
        // no. of rabbits in each group
        for (int a = 0; a < map.Count; a++) {
            int x = map.Keys.ElementAt(a);
            int y = map[x];
 
            // Find number of groups and
            // multiply them with number
            // of rabbits in each group
            if (y % (x + 1) == 0) {
                count = count + (y / (x + 1)) * (x + 1);
            }
            else
                count
                    = count + ((y / (x + 1)) + 1) * (x + 1);
        }
 
        // count gives minimum number
        // of rabbits in the forest
        return count;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int[] arr = { 2, 2, 0 };
        int N = arr.Length;
 
        // Function Call
        Console.WriteLine(minNumberOfRabbits(arr, N));
    }
}
 
// This code is contributed by AnkitRai01


Javascript


C++
// C++ program for the above approach
#include 
#include 
 
using namespace std;
 
// Function to find the minimum
// number of rabbits in the forest
int minNumberOfRabbits(int answers[], int n)
{
    // Initialize cnt variable
    int count = 0;
    // Initialize map
    unordered_map mp;
    for (int i = 0; i < n; ++i) {
 
        // Add to the count if not found or
        // has exhausted the count of all the
        // number of rabbits of same colour
        if (mp[answers[i]] == 0) {
            count += answers[i] + 1;
            mp[answers[i]] = answers[i] + 1;
        }
        mp[answers[i]]--;
    }
 
    // count gives minimum number
    // of rabbits in the forest
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 10, 10, 0 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << minNumberOfRabbits(arr, N) << endl;
 
    return 0;
}
 
// This code is contributed by Bhavna Soni - bhavna23


输出
4

时间复杂度: O(N)
辅助空间: O(N)

另一种解决方案是使用计数的 HashMap 并在每次遇到相同的答案 [i]时减少计数。如果相同的answers[i]再次出现并且计数为零,用answers[i]+1重置计数并添加到 answer 变量。以下是步骤:

  • 用零初始化变量计数
  • 使用无序映射 mp。
  • 遍历数组并执行以下操作:
    • 如果answers[i]设置为零,设置mp[answers[i]] = answers[i]+1count=count+answers[i]+1
    • 最后从地图中减去计数, mp[answers[i]]–
  • 返回 cnt 作为答案。

C++

// C++ program for the above approach
#include 
#include 
 
using namespace std;
 
// Function to find the minimum
// number of rabbits in the forest
int minNumberOfRabbits(int answers[], int n)
{
    // Initialize cnt variable
    int count = 0;
    // Initialize map
    unordered_map mp;
    for (int i = 0; i < n; ++i) {
 
        // Add to the count if not found or
        // has exhausted the count of all the
        // number of rabbits of same colour
        if (mp[answers[i]] == 0) {
            count += answers[i] + 1;
            mp[answers[i]] = answers[i] + 1;
        }
        mp[answers[i]]--;
    }
 
    // count gives minimum number
    // of rabbits in the forest
    return count;
}
 
// Driver Code
int main()
{
    int arr[] = { 10, 10, 0 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << minNumberOfRabbits(arr, N) << endl;
 
    return 0;
}
 
// This code is contributed by Bhavna Soni - bhavna23
输出
4

时间复杂度: O(N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程