森林里有一些彩色的兔子。给定的大小为N的阵列ARR [],使得具有ARR [i]表示兔子的数目相同的颜色的第i个兔子,任务是找到兔子,可能是在森林的最小数量。
例子:
Input: arr[] = {2, 2, 0}
Output: 4
Explanation: Considering the 1st and the 2nd rabbits to be of same color, eg. Blue, there should be 3 blue-colored rabbits. The third rabbit is the only rabbit of that color. Therefore, the minimum number of rabbits that could be present in the forest are = 3 + 1 = 4.
Input: arr[] = {10, 10, 10}
Output: 11
Explanation: Considering all the rabbits to be of the same color, the minimum number of rabbits present in forest are 10 + 1 = 11.
方法:解决此问题的方法是找到具有相同颜色的兔子的组数,以及每组中的兔子数。步骤如下:
- 初始化变量计数以存储每组中的兔子数量。
- 初始化映射,并遍历给定数组中键为arr [i]且值为arr [i]出现的数组。
- 现在,如果y个兔子回答x ,则:
- 如果(y%(x + 1))为0 ,则必须有(x / 1)只兔子(y /(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 minimun
// 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]]++;
}
// Intialize 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 minimun
// 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);
}
// Intialize 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 minimun
# 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
# Intialize 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 minimun
// 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);
}
// Intialize 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
C++
// C++ program for the above approach
#include
#include
using namespace std;
// Function to find the minimun
// 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)
另一种解决方案是使用计数的HasMap并在每次满足相同的答案[i]时减少计数。如果再次出现相同的答案[i]并且计数为零,请使用答案[i] +1重置计数,然后将其添加到答案变量。步骤如下:
- 用零初始化变量计数。
- 使用无序地图mp。
- 遍历数组并执行以下操作:
- 如果答案[i]设置为零,则设置mp [answers [i]] =答案[i] +1并计数= count + answers [i] +1
- 最后从地图中减去mp [answers [i]] – ;
- 返回cnt作为答案。
C++
// C++ program for the above approach
#include
#include
using namespace std;
// Function to find the minimun
// 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)