检查是否可以为 N 个对象着色,以便对于第 i 个对象,使用完全 arr[i] 不同的颜色
给定一个由N个正整数组成的数组arr[] ,任务是检查是否可以为N个对象着色,使得对于数组的第 i个元素,恰好存在用于为所有对象着色的arr[i]不同颜色除了第i个对象。
例子:
Input: arr[] = {1, 2, 2}
Output: Yes
Explanation:
One of the possible ways to color is: {“Red”, “blue”, “blue”}.
- For arr[0](=1), there is exactly 1 distinct color, which is “blue”.
- For arr[1](=2), there are exactly 2 distinct colors, which are “blue” and “red”.
- For arr[2](=3), there are exactly 2 distinct colors, which are “blue” and “red”.
Input: arr[] = {4, 3, 4, 3, 4}
Output: No
方法:可以根据以下观察解决问题:
- For 2 objects, there are N-2 objects common while calculating the number of distinct colors. Therefore, there can be a difference of at most 1 between the maximum and minimum element of the array arr[].
- Now there are two cases:
- If the maximum and minimum elements are equal, then the answer is “Yes”, only if the element is N – 1 or the element is less than or equal to N/2, because every color can be used more than once.
- If the difference between the maximum and minimum element is 1, then the number of distinct colors in the N object must be equal to the maximum element, because the minimum element is 1 less than the maximum element.
- Now, assuming the frequency of the minimum element as X and the frequency of the maximum element as Y, then the answer is “Yes” if and only if X+1 ≤ A ≤ X+ Y/2 (observation-based).
请按照以下步骤解决问题:
- 首先按升序对数组进行排序。
- 如果arr[N-1]和arr[0]的差大于1,则打印“ No ”。
- 否则,如果arr[N-1]等于arr[0] ,则检查以下内容:
- 如果arr[N-1] = N-1或2*arr[N-1] <= N ,则打印“是”。
- 否则,打印“否”。
- 否则,计算 min 和 max 元素的频率并将它们存储在变量中,比如X和Y ,然后执行以下操作:
- 如果arr[N-1]大于X并且arr[N-1]小于或等于X+Y/2 ,则打印“ Yes ”。
- 否则,打印“否”。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if coloring is
// possible with give conditions
string checkValid(int arr[], int N)
{
// Sort the vector
sort(arr, arr + N);
// Coloring not possible in case of
// maximum - minimum element > 1
if (arr[N - 1] - arr[0] > 1)
return "No";
// case 1
else if (arr[N - 1] == arr[0]) {
// If h is equal to N-1 or
// N is greater than 2*h
if (arr[N - 1] == N - 1
|| 2 * arr[N - 1] <= N)
return "Yes";
else
return "No";
}
// Case 2
else {
// Stores frequency of minimum element
int x = 0;
for (int i = 0; i < N; i++) {
// Frequency of minimum element
if (arr[i] == arr[0])
x++;
}
// Stores frequency of maximum element
int y = N - x;
// Condition for case 2
if ((arr[N - 1] >= x + 1)
and (arr[N - 1] <= x + y / 2))
return "Yes";
else
return "No";
}
}
// Driver Code
int main()
{
// GivenInput
int arr[] = { 1, 2, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << checkValid(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check if coloring is
// possible with give conditions
static String checkValid(int arr[], int N)
{
// Sort the vector
Arrays.sort(arr);
// Coloring not possible in case of
// maximum - minimum element > 1
if (arr[N - 1] - arr[0] > 1)
return "No";
// Case 1
else if (arr[N - 1] == arr[0])
{
// If h is equal to N-1 or
// N is greater than 2*h
if (arr[N - 1] == N - 1
|| 2 * arr[N - 1] <= N)
return "Yes";
else
return "No";
}
// Case 2
else
{
// Stores frequency of minimum element
int x = 0;
for(int i = 0; i < N; i++)
{
// Frequency of minimum element
if (arr[i] == arr[0])
x++;
}
// Stores frequency of maximum element
int y = N - x;
// Condition for case 2
if ((arr[N - 1] >= x + 1) &&
(arr[N - 1] <= x + y / 2))
return "Yes";
else
return "No";
}
}
// Driver Code
public static void main(String[] args)
{
// Given Input
int[] arr = { 1, 2, 2 };
int N = arr.length;
// Function Call
System.out.print(checkValid(arr, N));
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
# Function to check if coloring is
# possible with give conditions
def checkValid(arr, N):
# Sort the vector
arr = sorted(arr)
# Coloring not possible in case of
# maximum - minimum element > 1
if (arr[N - 1] - arr[0] > 1):
return "No"
# case 1
elif (arr[N - 1] == arr[0]):
# If h is equal to N-1 or
# N is greater than 2*h
if (arr[N - 1] == N - 1 or
2 * arr[N - 1] <= N):
return "Yes"
else:
return "No"
# Case 2
else:
# Stores frequency of minimum element
x = 0
for i in range(N):
# Frequency of minimum element
if (arr[i] == arr[0]):
x += 1
# Stores frequency of maximum element
y = N - x
# Condition for case 2
if ((arr[N - 1] >= x + 1) and
(arr[N - 1] <= x + y // 2)):
return "Yes"
else:
return "No"
# Driver Code
if __name__ == '__main__':
# Given Input
arr = [ 1, 2, 2 ]
N = len(arr)
# Function Call
print (checkValid(arr, N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to check if coloring is
// possible with give conditions
static string checkValid(int []arr, int N)
{
// Sort the vector
Array.Sort(arr);
// Coloring not possible in case of
// maximum - minimum element > 1
if (arr[N - 1] - arr[0] > 1)
return "No";
// case 1
else if (arr[N - 1] == arr[0]) {
// If h is equal to N-1 or
// N is greater than 2*h
if (arr[N - 1] == N - 1
|| 2 * arr[N - 1] <= N)
return "Yes";
else
return "No";
}
// Case 2
else {
// Stores frequency of minimum element
int x = 0;
for (int i = 0; i < N; i++) {
// Frequency of minimum element
if (arr[i] == arr[0])
x++;
}
// Stores frequency of maximum element
int y = N - x;
// Condition for case 2
if ((arr[N - 1] >= x + 1)
&& (arr[N - 1] <= x + y / 2))
return "Yes";
else
return "No";
}
}
// Driver Code
public static void Main()
{
// GivenInput
int []arr = { 1, 2, 2 };
int N = arr.Length;
// Function Call
Console.Write(checkValid(arr, N));
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出
Yes
时间复杂度: O(N*log(N))
辅助空间: O(1)