给定 N 个糖果,它可以有多种不同类型和 k 个顾客,一个顾客不会制作超过 2 件的同一种糖果,任务是寻找是否可以分发所有糖果,然后打印“是”或否则“否”。
给定一个数组,arr[] 表示一个糖果数组。 arr[i] 是一种 Sweet 。
例子:
Input : arr[] = {1, 1, 2, 3, 1},
k = 2;
Output : Yes
There are three pieces of sweet type 1,
one piece of type 2 and one piece of
type 3. Two customers can distribute
sweets under given constraints.
Input : arr[] = {2, 3, 3, 5, 3, 3},
k = 2;
Output : Yes
Input : arr[] = {2, 3, 3, 5, 3, 3, 3},
k = 2;
Output : No
方法一:
1- 遍历每个元素的数组。
2- 计算数组中每个元素的出现次数
3- 检查每个元素的结果是否必须小于或等于 2*k。
C++
// C++ program for above implementation
#include
using namespace std;
// Function to check occurrence of each element
bool checkCount(int arr[], int n, int k)
{
int count;
// Start traversing the elements
for (int i = 0; i < n; i++) {
// Count occurrences of current element
count = 0;
for (int j = 0; j < n; j++) {
if (arr[j] == arr[i])
count++;
// If count of any element is greater
// than 2*k then return false
if (count > 2 * k)
return false;
}
}
return true;
}
// Drivers code
int main()
{
int arr[] = { 1, 1, 2, 3, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
int k = 2;
checkCount(arr, n, k) ? cout << "Yes"
: cout << "No";
return 0;
}
Java
// java program for above implementation
import java.io.*;
public class GFG {
// Function to check occurrence of
// each element
static boolean checkCount(int []arr,
int n, int k)
{
int count;
// Start traversing the elements
for (int i = 0; i < n; i++)
{
// Count occurrences of
// current element
count = 0;
for (int j = 0; j < n; j++)
{
if (arr[j] == arr[i])
count++;
// If count of any element
// is greater than 2*k then
// return false
if (count > 2 * k)
return false;
}
}
return true;
}
// Drivers code
static public void main (String[] args)
{
int []arr = { 1, 1, 2, 3, 1 };
int n = arr.length;
int k = 2;
if(checkCount(arr, n, k))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by vt_m.
Python3
# Python 3 program for above implementation
# Function to check occurrence
# of each element
def checkCount(arr, n, k):
# Start traversing the elements
for i in range(n):
# Count occurrences of
# current element
count = 0
for j in range(n):
if arr[j] == arr[i]:
count += 1
# If count of any element is greater
# than 2*k then return false
if count > 2 * k:
return False
return True
# Driver code
arr = [1, 1, 2, 3, 1]
n = len(arr)
k = 2
if checkCount(arr, n, k) == True:
print("Yes")
else:
print("No")
# This code is contributed by Shrikant13
C#
// C# program for above implementation
using System;
public class GFG {
// Function to check occurrence
// of each element
static bool checkCount(int []arr,
int n, int k)
{
int count;
// Start traversing the elements
for (int i = 0; i < n; i++)
{
// Count occurrences of
// current element
count = 0;
for (int j = 0; j < n; j++)
{
if (arr[j] == arr[i])
count++;
// If count of any element
// is greater than 2*k then
// return false
if (count > 2 * k)
return false;
}
}
return true;
}
// Drivers code
static public void Main ()
{
int []arr = { 1, 1, 2, 3, 1 };
int n = arr.Length;
int k = 2;
if(checkCount(arr, n, k))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by vt_m.
PHP
2 * $k)
return false;
}
}
return true;
}
// Driver Code
$arr = array(1, 1, 2, 3, 1);
$n =count($arr);
$k = 2;
if(checkCount($arr, $n, $k))
echo "Yes";
else
echo "No";
// This code is contributed by anuj_67.
?>
Javascript
C++
// C++ program for above implementation
#include
using namespace std;
// Function to check hash array
// corresponding to the given array
bool checkCount(int arr[], int n, int k)
{
unordered_map hash;
// Maintain a hash
for (int i = 0; i < n; i++)
hash[arr[i]]++;
// Check for each value in hash
for (auto x : hash)
if (x.second > 2 * k)
return false;
return true;
}
// Drivers code
int main()
{
int arr[] = { 1, 1, 2, 3, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
int k = 2;
checkCount(arr, n, k) ? cout << "Yes"
: cout << "No";
return 0;
}
Java
// Java program for above implementation
import java.util.HashMap;
import java.util.Map;
class GfG
{
// Function to check hash array
// corresponding to the given array
static boolean checkCount(int arr[], int n, int k)
{
HashMap hash = new HashMap<>();
// Maintain a hash
for (int i = 0; i < n; i++)
{
if (!hash.containsKey(arr[i]))
hash.put(arr[i], 0);
hash.put(arr[i], hash.get(arr[i]) + 1);
}
// Check for each value in hash
for (Map.Entry x : hash.entrySet())
if ((int)x.getValue() > 2 * k)
return false;
return true;
}
// Driver code
public static void main(String []args)
{
int arr[] = { 1, 1, 2, 3, 1 };
int n = arr.length;
int k = 2;
if (checkCount(arr, n, k))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Rituraj Jain
Python3
# Python3 program for above implementation
from collections import defaultdict
# Function to check hash array
# corresponding to the given array
def checkCount(arr, n, k):
mp = defaultdict(lambda:0)
# Insert all array elements in
# hash table Maintain a hash
for i in range(n):
mp[arr[i]] += 1
# Check for each value in hash
for key, values in mp.items():
if values > 2 * k:
return False
return True
# Driver code
arr = [ 1, 1, 2, 3, 1 ]
n = len(arr)
k = 2
if checkCount(arr, n, k) == True:
print("Yes")
else:
print("No")
# This code is contributed by Shrikant13
C#
// C# program for above implementation
using System;
using System.Collections.Generic;
class GfG
{
// Function to check hash array
// corresponding to the given array
static Boolean checkCount(int []arr, int n, int k)
{
Dictionary hash = new Dictionary();
// Maintain a hash
for (int i = 0; i < n; i++)
{
if(hash.ContainsKey(arr[i]))
{
var val = hash[arr[i]];
hash.Remove(arr[i]);
hash.Add(arr[i], val + 1);
}
else
{
hash.Add(arr[i], 0);
}
}
// Check for each value in hash
foreach(KeyValuePair x in hash)
if ((int)x.Value > 2 * k)
return false;
return true;
}
// Driver code
public static void Main(String []args)
{
int []arr = { 1, 1, 2, 3, 1 };
int n = arr.Length;
int k = 2;
if (checkCount(arr, n, k))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
/* This code is contributed by PrinciRaj1992 */
输出:
Yes
时间复杂度: O(n^2)
方法二:
1. 维护 32 种不同类型糖果的哈希值。
2.遍历一个数组并检查每个arr[i]
hash[arr[i]] <= 2*k.
C++
// C++ program for above implementation
#include
using namespace std;
// Function to check hash array
// corresponding to the given array
bool checkCount(int arr[], int n, int k)
{
unordered_map hash;
// Maintain a hash
for (int i = 0; i < n; i++)
hash[arr[i]]++;
// Check for each value in hash
for (auto x : hash)
if (x.second > 2 * k)
return false;
return true;
}
// Drivers code
int main()
{
int arr[] = { 1, 1, 2, 3, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
int k = 2;
checkCount(arr, n, k) ? cout << "Yes"
: cout << "No";
return 0;
}
Java
// Java program for above implementation
import java.util.HashMap;
import java.util.Map;
class GfG
{
// Function to check hash array
// corresponding to the given array
static boolean checkCount(int arr[], int n, int k)
{
HashMap hash = new HashMap<>();
// Maintain a hash
for (int i = 0; i < n; i++)
{
if (!hash.containsKey(arr[i]))
hash.put(arr[i], 0);
hash.put(arr[i], hash.get(arr[i]) + 1);
}
// Check for each value in hash
for (Map.Entry x : hash.entrySet())
if ((int)x.getValue() > 2 * k)
return false;
return true;
}
// Driver code
public static void main(String []args)
{
int arr[] = { 1, 1, 2, 3, 1 };
int n = arr.length;
int k = 2;
if (checkCount(arr, n, k))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Rituraj Jain
蟒蛇3
# Python3 program for above implementation
from collections import defaultdict
# Function to check hash array
# corresponding to the given array
def checkCount(arr, n, k):
mp = defaultdict(lambda:0)
# Insert all array elements in
# hash table Maintain a hash
for i in range(n):
mp[arr[i]] += 1
# Check for each value in hash
for key, values in mp.items():
if values > 2 * k:
return False
return True
# Driver code
arr = [ 1, 1, 2, 3, 1 ]
n = len(arr)
k = 2
if checkCount(arr, n, k) == True:
print("Yes")
else:
print("No")
# This code is contributed by Shrikant13
C#
// C# program for above implementation
using System;
using System.Collections.Generic;
class GfG
{
// Function to check hash array
// corresponding to the given array
static Boolean checkCount(int []arr, int n, int k)
{
Dictionary hash = new Dictionary();
// Maintain a hash
for (int i = 0; i < n; i++)
{
if(hash.ContainsKey(arr[i]))
{
var val = hash[arr[i]];
hash.Remove(arr[i]);
hash.Add(arr[i], val + 1);
}
else
{
hash.Add(arr[i], 0);
}
}
// Check for each value in hash
foreach(KeyValuePair x in hash)
if ((int)x.Value > 2 * k)
return false;
return true;
}
// Driver code
public static void Main(String []args)
{
int []arr = { 1, 1, 2, 3, 1 };
int n = arr.Length;
int k = 2;
if (checkCount(arr, n, k))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
/* This code is contributed by PrinciRaj1992 */
输出:
Yes
时间复杂度: O(n)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。