📌  相关文章
📜  检查给定数组的 N 个索引是否可以使用最多 K 次使用一种颜色被 M 种颜色着色

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

检查给定数组的 N 个索引是否可以使用最多 K 次使用一种颜色被 M 种颜色着色

N个索引找到M种颜色的排列,使得没有两个相邻的索引具有相同的颜色,并且每种颜色最多可以使用K次。如果不存在这样的安排,则输出 -1。

例子:

方法:注意以下几点:

  • 如果颜色数只有 1 且索引数大于 1,则不存在这样的分配。
  • 如果索引的总数大于可用颜色( M * K )可以着色的数量,则不存在此类分配。

按照以下步骤解决上述问题:

  • 如果颜色数为 1 且索引数大于 1,则输出 -1。
  • 如果颜色总数大于可用颜色( M * K )可以着色的颜色,则输出 -1。
  • 如果以上两个条件都不满足,那么:
    • 用 1 初始化变量x
    • 运行一个循环直到N并在循环内输出x 。增加x并检查x是否大于M 。如果x变得大于M ,则设置x = 1。
    • x设置为 1 时,循环再次从 1 开始打印颜色数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the required
// arrangement of colors to indices
void findAssignment(int N, int M, int K)
{
    // If number of colors is 1
    // and number of indices is more than 1
    if (M == 1 && N > 1) {
 
        // Output -1
        cout << -1;
    }
 
    // If number of indices is more than
    // what can be colored
    // by the available colors
    else if (N > M * K) {
 
        // Output -1
        cout << -1;
    }
    else {
 
        // Initialize x with 1
        int x = 1;
 
        // Loop to print
        // the required arrangement
        for (int i = 0; i < N; ++i) {
 
            // Output the number of colors
            cout << x << " ";
 
            // Increment the number of colors
            x++;
 
            // If x exceeds the total number
            // of available colors
            if (x > M) {
 
                // Set x to 1
                x = 1;
            }
        }
    }
}
 
// Driver Code
int main()
{
    // Given input
    int N = 6, M = 4, K = 2;
 
    // Function Call
    findAssignment(N, M, K);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to find the required
  // arrangement of colors to indices
  static void findAssignment(int N, int M, int K)
  {
 
    // If number of colors is 1
    // and number of indices is more than 1
    if (M == 1 && N > 1) {
 
      // Output -1
      System.out.print("-1");
    }
 
    // If number of indices is more than
    // what can be colored
    // by the available colors
    else if (N > M * K) {
 
      // Output -1
      System.out.print("-1");
    }
    else {
 
      // Initialize x with 1
      int x = 1;
 
      // Loop to print
      // the required arrangement
      for (int i = 0; i < N; ++i) {
 
        // Output the number of colors
        System.out.print(x + " ");
 
        // Increment the number of colors
        x++;
 
        // If x exceeds the total number
        // of available colors
        if (x > M) {
 
          // Set x to 1
          x = 1;
        }
      }
    }
  }
 
  // Driver Code
  public static void main (String[] args)
  {
 
    // Given input
    int N = 6, M = 4, K = 2;
 
    // Function Call
    findAssignment(N, M, K);
  }
}
 
// This code is contributed by hrithikgarg03188


Python3
# Python code for the above approach
 
# Function to find the required
# arrangement of colors to indices
def findAssignment(N, M, K):
 
    # If number of colors is 1
    # and number of indices is more than 1
    if (M == 1 and N > 1):
 
        # Output -1
        print(-1)
     
    # If number of indices is more than
    # what can be colored
    # by the available colors
    elif (N > M * K) :
 
        # Output -1
        print(-1)
     
    else:
 
        # Initialize x with 1
        x = 1;
 
        # Loop to print
        # the required arrangement
        for i in range(N):
 
            # Output the number of colors
            print(x, end= " ");
 
            # Increment the number of colors
            x += 1
 
            # If x exceeds the total number
            # of available colors
            if (x > M):
 
                # Set x to 1
                x = 1;
 
# Driver Code
 
# Given input
N = 6
M = 4
K = 2
 
# Function Call
findAssignment(N, M, K);
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
class GFG
{
   
// Function to find the required
// arrangement of colors to indices
static void findAssignment(int N, int M, int K)
{
   
    // If number of colors is 1
    // and number of indices is more than 1
    if (M == 1 && N > 1) {
 
        // Output -1
        Console.Write(-1);
    }
 
    // If number of indices is more than
    // what can be colored
    // by the available colors
    else if (N > M * K) {
 
        // Output -1
        Console.Write(-1);
    }
    else {
 
        // Initialize x with 1
        int x = 1;
 
        // Loop to print
        // the required arrangement
        for (int i = 0; i < N; ++i) {
 
            // Output the number of colors
            Console.Write(x + " ");
 
            // Increment the number of colors
            x++;
 
            // If x exceeds the total number
            // of available colors
            if (x > M) {
 
                // Set x to 1
                x = 1;
            }
        }
    }
}
 
// Driver Code
public static void Main()
{
   
    // Given input
    int N = 6, M = 4, K = 2;
 
    // Function Call
    findAssignment(N, M, K);
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
1 2 3 4 1 2 

时间复杂度: 在)
辅助空间: O(1)