📜  检查是否可以为 N 个对象着色,以便对于第 i 个对象,使用完全 arr[i] 不同的颜色

📅  最后修改于: 2022-05-13 01:56:07.519000             🧑  作者: Mango

检查是否可以为 N 个对象着色,以便对于第 i 个对象,使用完全 arr[i] 不同的颜色

给定一个由N个正整数组成的数组arr[] ,任务是检查是否可以为N个对象着色,使得对于数组的第 i元素,恰好存在用于为所有对象着色的arr[i]不同颜色除了第i对象。

例子:

方法:可以根据以下观察解决问题:

请按照以下步骤解决问题:

  • 首先按升序对数组进行排序。
  • 如果arr[N-1]arr[0]的差大于1,则打印“ No ”。
  • 否则,如果arr[N-1]等于arr[0] ,则检查以下内容:
    • 如果arr[N-1] = N-12*arr[N-1] <= N ,则打印“”。
    • 否则,打印“”。
  • 否则,计算 min 和 max 元素的频率并将它们存储在变量中,比如XY ,然后执行以下操作:
    • 如果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)