给定一个包含N个整数的圆形数组arr [] ,任务是找到使数组元素着色所需的最小颜色数,以使两个具有不同值的相邻元素不能被着色相同。
例子:
Input: arr[] = {1, 2, 1, 1, 2}
Output: 2
Explanation:
Minimum 2 type of colors are required.
We can distribute color as {r, g, r, r, g} such that no adjacent element having different value are colored same.
Input: arr[] = {1, 2, 3, 4}
Output: 2
Explanation:
Minimum 2 type of colors are required.
We can distribute color as {r, g, r, g}.
方法:此问题可以使用贪婪方法解决。
- 如果所有值都相同,则仅需要一种颜色。
- 如果有多个不同的元素,并且元素总数为偶数,则需要2种颜色。
- 如果有多个不同的元素,并且元素总数为奇数,请检查:
- 如果存在具有相同值的相邻元素,则需要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
输出:
2
时间复杂度: O(N) ,其中N是元素数。