给定一个包含N 个糖果的数组arr[] ,其中N是偶数, arr[i]表示糖果的类型。任务是找到一个人可以吃的不同类型糖果的最大数量,如果只有N/2个可以吃。
例子:
Input: arr[] = {4, 4, 5, 5, 6, 6, 7, 7}
Output: 4
Explanation: Person can only eat 8/2 = 4 candies and there are 4 types of candies in the array. Thus, the person can eat one candy of each type.
Input: arr[] = {2, 2, 3, 1}
Output: 2
Explanation: Person can only eat 4/2 = 2 and there are 3 types of candies in the array. Thus, the answer is 2, since maximum 2 candies allowed.
朴素的方法:这个想法是在给定的数组中找到糖果类型的数量。如果允许吃的最大糖果数量大于给定的糖果类型数量,那么答案是给定的糖果类型数量。否则,答案是允许食用的最大糖果数量。
时间复杂度: O(N 2 )
辅助空间: O(1)
有效的方法:这个想法是使用散列。请按照以下步骤解决问题:
- 初始化一个 hashset s来存储唯一的糖果类型。
- 遍历数组arr[]并插入集合s中的所有元素。
- 将哈希集s的大小存储在变量M 中。
- 如果(M < N/2) 的值,则打印M ,否则打印N/2作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find number of candy types
int num_candyTypes(vector& candies)
{
// Declare a hashset to store candies
unordered_set s;
// Traverse the given array and
// inserts element into set
for (int i = 0; i < candies.size(); i++) {
s.insert(candies[i]);
}
// Return the result
return s.size();
}
// Function to find maximum number of
// types of candies a person can eat
void distribute_candies(vector& candies)
{
// Store the number of candies
// allowed to eat
int allowed = candies.size() / 2;
// Store the number of candy types
int types = num_candyTypes(candies);
// Return the result
if (types < allowed)
cout << types;
else
cout << allowed;
}
// Driver Code
int main()
{
// Given Input
vector candies = { 4, 4, 5, 5, 3, 3 };
// Function Call
distribute_candies(candies);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find number of candy types
public static int num_candyTypes(int []candies)
{
// Declare a hashset to store candies
Dictionary s = new Hashtable();
// Traverse the given array and
// inserts element into set
for(int i = 0; i < candies.length; i++)
{
s.put(candies[i], 1);
}
// Return the result
return s.size();
}
// Function to find maximum number of
// types of candies a person can eat
public static void distribute_candies(int []candies)
{
// Store the number of candies
// allowed to eat
int allowed = candies.length / 2;
// Store the number of candy types
int types = num_candyTypes(candies);
// Return the result
if (types < allowed)
System.out.println(types);
else
System.out.println(allowed);
}
// Driver code
public static void main(String[] args)
{
// Given Input
int candies[] = { 4, 4, 5, 5, 3, 3 };
// Function Call
distribute_candies(candies);
}
}
// This code is contributed by mohit kumar 29
Python3
# python 3 program for the above approach
# Function to find number of candy types
def num_candyTypes(candies):
# Declare a hashset to store candies
s = set()
# Traverse the given array and
# inserts element into set
for i in range(len(candies)):
s.add(candies[i])
# Return the result
return len(s)
# Function to find maximum number of
# types of candies a person can eat
def distribute_candies(candies):
# Store the number of candies
# allowed to eat
allowed = len(candies)/2
# Store the number of candy types
types = num_candyTypes(candies)
# Return the result
if (types < allowed):
print(int(types))
else:
print(int(allowed))
# Driver Code
if __name__ == '__main__':
# Given Input
candies = [4, 4, 5, 5, 3, 3]
# Function Call
distribute_candies(candies)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG{
// Function to find number of candy types
public static int num_candyTypes(int []candies)
{
// Declare a hashset to store candies
Dictionary s = new Dictionary();
// Traverse the given array and
// inserts element into set
for(int i = 0; i < candies.Length; i++)
{
if(!s.ContainsKey(candies[i]))
s.Add(candies[i], 1);
}
// Return the result
return s.Count;
}
// Function to find maximum number of
// types of candies a person can eat
public static void distribute_candies(int []candies)
{
// Store the number of candies
// allowed to eat
int allowed = candies.Length / 2;
// Store the number of candy types
int types = num_candyTypes(candies);
// Return the result
if (types < allowed)
Console.WriteLine(types);
else
Console.WriteLine(allowed);
}
// Driver code
static public void Main (){
// Given Input
int[] candies = { 4, 4, 5, 5, 3, 3 };
// Function Call
distribute_candies(candies);
}
}
// This code is contributed by unknown2108.
Javascript
3
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。