📜  一组中没有重复的 RGB 球的排列计数

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

一组中没有重复的 RGB 球的排列计数

给定R红球, G绿球, B蓝球。一组可以有1 个2 个3个球。此外,一组可以有所有相同颜色的球或所有不同颜色的球。所有其他可能的集合都被认为是无效的。任务是计算放置所有球所需的最小可能组数。

例子:

方法:这个问题是基于分析的。请按照以下步骤解决给定的问题。

  • 问题陈述可以通过仔细的案例分析来解决。
  • 不失一般性假设R<=G<=B。
  • 所以至少会形成 R 个集合。从 G 和 B 中减去 R。从这一步形成的所有集合中都有不同的球。
  • 剩余的球将是0, G – R, B – R 。对于剩余的球形成相同球的组。
  • 完成上述步骤后,剩余球数将为0,(G – R)%3,(B – R)%3。
  • 现在可能有3种情况:
  • 第 2 项为 0,第 3 项为 0。不需要额外的套装。
  • (第 2 项为 1,第 3 项为 1)或(第 2 项为 0,第 3 项为 2,反之亦然)。需要额外的一套。
  • 在所有其他情况下,将需要另外 2 套。
  • 仔细检查以上所有条件并打印答案。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to calculate minimum sets
// required with given conditions
int minimumSetBalls(int R, int G, int B)
{
    // Push values R, G, B
    // in a vector and sort them
    vector balls;
    balls.push_back(R);
    balls.push_back(G);
    balls.push_back(B);
 
    // Sort the vector
    sort(balls.begin(), balls.end());
 
    // Store the answer
    int ans = 0;
    ans += balls[0];
    balls[1] -= balls[0];
    balls[2] -= balls[0];
    balls[0] = 0;
 
    // Check all mentioned conditions
    ans += balls[1] / 3;
    balls[1] %= 3;
    ans += balls[2] / 3;
    balls[2] %= 3;
 
    if (balls[1] == 0
        && balls[2] == 0) {
        // No extra set required
    }
    else if (balls[1] == 1
             && balls[2] == 1) {
        ans++;
        // 1 extra set is required
    }
    else if ((balls[1] == 2
              && balls[2] == 0)
             || (balls[1] == 0
                 && balls[2] == 2)) {
        ans++;
        // 1 extra set is required
    }
    else {
        // 2 extra sets will be required
        ans += 2;
    }
    return ans;
}
 
// Driver Code
int main()
{
    int R = 4, G = 2, B = 4;
    cout << minimumSetBalls(R, G, B);
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
public class GFG{
 
// Function to calculate minimum sets
// required with given conditions
static int minimumSetBalls(int R, int G, int B)
{
   
    // Push values R, G, B
    // in a vector and sort them
    ArrayList balls = new ArrayList();
    balls.add(R);
    balls.add(G);
    balls.add(B);
   
    // Sort the vector
    Collections.sort(balls);   ;
   
    // Store the answer
    int ans = 0;
    ans += balls.get(0);
    balls.set(1,balls.get(1) - balls.get(0));
    balls.set(2,balls.get(2) - balls.get(0));
    balls.set(0,0);
   
    // Check all mentioned conditions
    ans += balls.get(1) / 3;
    balls.set(1, balls.get(1) % 3);
    ans += balls.get(2) / 3;
    balls.set(2,balls.get(2) % 3);
   
    if (balls.get(1) == 0
        && balls.get(2) == 0)
    {
       
        // No extra set required
    }
    else if (balls.get(1) == 1
             && balls.get(2) == 1) {
        ans++;
        // 1 extra set is required
    }
    else if ((balls.get(1) == 2
              && balls.get(2) == 0)
             || (balls.get(1) == 0
                 && balls.get(2) == 2)) {
        ans++;
        // 1 extra set is required
    }
    else {
        // 2 extra sets will be required
        ans += 2;
    }
    return ans;
}
 
 
// Driver Code
public static void main(String []args)
{
    int R = 4, G = 2, B = 4;
    System.out.println( minimumSetBalls(R, G, B));
}
}
 
// This code is contributed by AnkThon


Python3
# Python3 Program to implement the above approach
 
# Function to calculate minimum sets
# required with given conditions
def minimumSetBalls(R, G, B) :
 
    # Push values R, G, B
    # in a vector and sort them
    balls = [];
    balls.append(R);
    balls.append(G);
    balls.append(B);
 
    # Sort the vector
    balls.sort();
 
    # Store the answer
    ans = 0;
    ans += balls[0];
    balls[1] -= balls[0];
    balls[2] -= balls[0];
    balls[0] = 0;
 
    # Check all mentioned conditions
    ans += balls[1] // 3;
    balls[1] %= 3;
    ans += balls[2] // 3;
    balls[2] %= 3;
 
    if (balls[1] == 0 and balls[2] == 0) :
        pass
     
    elif (balls[1] == 1 and balls[2] == 1) :
        ans += 1;
        # 1 extra set is required
 
    elif ((balls[1] == 2 and balls[2] == 0) or (balls[1] == 0 and balls[2] == 2)) :
        ans += 1;
        # 1 extra set is required
    else :
        # 2 extra sets will be required
        ans += 2;
 
    return ans;
 
# Driver Code
if __name__ == "__main__" :
 
    R = 4; G = 2; B = 4;
    print(minimumSetBalls(R, G, B));
     
    # This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to calculate minimum sets
// required with given conditions
static int minimumSetBalls(int R, int G, int B)
{
   
    // Push values R, G, B
    // in a vector and sort them
    List balls = new List();
    balls.Add(R);
    balls.Add(G);
    balls.Add(B);
   
    // Sort the vector
    balls.Sort();
   
    // Store the answer
    int ans = 0;
    ans += balls[0];
    balls[1] -= balls[0];
    balls[2] -= balls[0];
    balls[0] = 0;
   
    // Check all mentioned conditions
    ans += balls[1] / 3;
    balls[1] %= 3;
    ans += balls[2] / 3;
    balls[2] %= 3;
   
    if (balls[1] == 0
        && balls[2] == 0)
    {
       
        // No extra set required
    }
    else if (balls[1] == 1
             && balls[2] == 1) {
        ans++;
        // 1 extra set is required
    }
    else if ((balls[1] == 2
              && balls[2] == 0)
             || (balls[1] == 0
                 && balls[2] == 2)) {
        ans++;
        // 1 extra set is required
    }
    else {
        // 2 extra sets will be required
        ans += 2;
    }
    return ans;
}
 
 
// Driver Code
public static void Main()
{
    int R = 4, G = 2, B = 4;
    Console.WriteLine( minimumSetBalls(R, G, B));
}
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
4

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