📜  为圆形阵列着色所需的最少颜色数

📅  最后修改于: 2021-10-27 03:25:52             🧑  作者: Mango

给定一个包含N 个整数的圆形数组arr[] ,任务是找到为数组元素着色所需的最小颜色数,使得具有不同值的两个相邻元素不能被着色相同。
例子:

方法:这个问题可以使用贪心方法来解决。

  1. 如果所有值都相同,则只需要 1 种颜色。
  2. 如果有多个不同的元素并且元素总数是偶数,则需要 2 种颜色。
  3. 如果有多个不同的元素并且元素的总数是奇数,则检查:
    • 如果存在具有相同值的相邻元素,则需要 2 种颜色。
    • 否则需要3种颜色。

下面是上述方法的实现:

C++
// C++ implementation of above approach
 
#include 
using namespace std;
 
// Function that finds minimum number of
// colors required
void colorRequired(int arr[], int n)
{
 
    // To check that if all the elements
    // are same or not
    bool all_same = true;
 
    // To check if only one adjacent exist
    bool one_adjacent_same = false;
 
    // Traverse the array
    for (int i = 0; i < n - 1; i++) {
 
        // If adjacent elements found
        // different means all are not
        // same
        if (arr[i] != arr[i + 1]) {
            all_same = false;
        }
 
        // If two adjacent elements found
        // to be same then make
        // one_adjacent_same true
        if (arr[i] == arr[i + 1]) {
            one_adjacent_same = true;
        }
    }
 
    // If all elements are same
    // then print 1
    if (all_same == true) {
        cout << 1 << endl;
        return;
    }
 
    // If total number of elements are
    // even or there exist two adjacent
    // elements that are same
    // then print 2
    if (n % 2 == 0
        || one_adjacent_same == true) {
        cout << 2 << endl;
        return;
    }
 
    // Else 3 type of colors
    // are required
    cout << 3 << endl;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 1, 1, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    colorRequired(arr, n);
    return 0;
}


Java
// Java implementation of above approach
import java.util.*;
 
class GFG{
 
// Function that finds minimum number of
// colors required
static void colorRequired(int arr[], int n)
{
 
    // To check that if all the elements
    // are same or not
    boolean all_same = true;
 
    // To check if only one adjacent exist
    boolean one_adjacent_same = false;
 
    // Traverse the array
    for(int i = 0; i < n - 1; i++)
    {
        
       // If adjacent elements found
       // different means all are not
       // same
       if (arr[i] != arr[i + 1])
       {
           all_same = false;
       }
        
       // If two adjacent elements found
       // to be same then make
       // one_adjacent_same true
       if (arr[i] == arr[i + 1])
       {
           one_adjacent_same = true;
       }
    }
 
    // If all elements are same
    // then print 1
    if (all_same == true)
    {
        System.out.print(1 + "\n");
        return;
    }
 
    // If total number of elements are
    // even or there exist two adjacent
    // elements that are same
    // then print 2
    if (n % 2 == 0 ||
        one_adjacent_same == true)
    {
        System.out.print(2 + "\n");
        return;
    }
 
    // Else 3 type of colors
    // are required
    System.out.print(3 + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 1, 1, 2 };
    int n = arr.length;
 
    // Function call
    colorRequired(arr, n);
}
}
 
// This code is contributed by Rohit_ranjan


Python3
# Python3 implementation of the above approach
 
# Function that finds minimum number
# of colors required
def colorRequired(arr, n):
 
    # To check that if all the elements
    # are same or not
    all_same = True
 
    # To check if only one adjacent exist
    one_adjacent_same = False
 
    # Traverse the array
    for i in range(n - 1):
 
        # If adjacent elements found
        # different means all are not
        # same
        if(arr[i] != arr[i + 1]):
            all_same = False
 
        # If two adjacent elements
        # found to be same then make
        # one_adjacent_same true
        if(arr[i] == arr[i + 1]):
            one_adjacent_same = True
 
    # If all elements are same
    # then print 1
    if(all_same == True):
        print(1)
        return
 
    # If total number of elements are
    # even or there exist two adjacent
    # elements that are same
    # then print 2
    if(n % 2 == 0 or one_adjacent_same == True):
        print(2)
        return
 
    # Else 3 type of colors
    # are required
    print(3)
 
# Driver Code
if __name__ == '__main__':
 
    arr = [ 1, 2, 1, 1, 2 ]
    n = len(arr)
 
    # Function call
    colorRequired(arr, n)
 
# This code is contributed by Shivam Singh


C#
// C# implementation of above approach
using System;
class GFG{
 
// Function that finds minimum number of
// colors required
static void colorRequired(int []arr, int n)
{
 
    // To check that if all the elements
    // are same or not
    bool all_same = true;
 
    // To check if only one adjacent exist
    bool one_adjacent_same = false;
 
    // Traverse the array
    for(int i = 0; i < n - 1; i++)
    {
         
        // If adjacent elements found
        // different means all are not
        // same
        if (arr[i] != arr[i + 1])
        {
            all_same = false;
        }
             
        // If two adjacent elements found
        // to be same then make
        // one_adjacent_same true
        if (arr[i] == arr[i + 1])
        {
            one_adjacent_same = true;
        }
    }
 
    // If all elements are same
    // then print 1
    if (all_same == true)
    {
        Console.Write(1 + "\n");
        return;
    }
 
    // If total number of elements are
    // even or there exist two adjacent
    // elements that are same
    // then print 2
    if (n % 2 == 0 ||
        one_adjacent_same == true)
    {
        Console.Write(2 + "\n");
        return;
    }
 
    // Else 3 type of colors
    // are required
    Console.Write(3 + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 1, 1, 2 };
    int n = arr.Length;
 
    // Function call
    colorRequired(arr, n);
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
2

时间复杂度: O(N) ,其中 N 是元素的数量。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程