📌  相关文章
📜  检查是否可以用相同的球完全填充每个容器

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

检查是否可以用相同的球完全填充每个容器

给定两个数组,容器的 arr [ ] C和球的arr[ ] B ,如果每个容器只能存储相同类型的球任务是找出是否有可能用给定的球完全填充每个容器。在数组 C中, C[i]存储第 i 个容器可以存储的最大球数。在数组 B 中,B[i]存储第 i 个球的类型。

例子:

方法:这个想法是使用回溯来检查是否可以填充每个容器。可以观察到只需要每种球的频率,所以我们将每种球的频率存储在一个map中。让我们看看实施我们的方法所涉及的步骤:

  • 将相同类型的球的频率存储在地图中。
  • 调用函数getans以检查是否可以填充容器。
  • 尝试用频率大于等于容器容量的球填充容器。如果它可能返回否则回溯并检查其他球。
  • 如果不存在组合,则返回false

下面是上述方法的实现。

C++
// C++ program for above approach
#include 
using namespace std;
 
// A boolean function that returns true if it's possible to
// completely fill the container else return false
bool getans(int i, vector v, vector q)
{
    // Base Case
    if (i == q.size())
        return true;
 
    // Backtracking
    for (int j = 0; j < v.size(); j++) {
        if (v[j] >= q[i]) {
            v[j] -= q[i];
            if (getans(i + 1, v, q)) {
                return true;
            }
            v[j] += q[i];
        }
    }
    return false;
}
 
// Function to check the conditions
void Check(vector c, vector b)
{
 
    // Storing frequencies
    map m;
    for (int i = 0; i < b.size(); i++) {
        m[b[i]]++;
    }
    vector v;
    for (auto i : m) {
        v.push_back(i.second);
    }
 
    // Function Call for backtracking
    bool check = getans(0, v, c);
    if (check)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}
 
// Driver Code
int main()
{
    // Given Input
    vector c = { 1, 3, 3 };
    vector b = { 2, 2, 2, 2, 4, 4, 4 };
 
    // Function Call
    Check(c, b);
    return 0;
}


Python3
# Python 3 program for above approach
 
# A boolean function that returns true if it's possible to
# completely fill the container else return false
def getans(i, v, q):
   
    # Base Case
    if (i == len(q)):
        return True
 
    # Backtracking
    for j in range(len(v)):
        if(v[j] >= q[i]):
            v[j] -= q[i]
            if (getans(i + 1, v, q)):
                return True
            v[j] += q[i]
    return False
 
# Function to check the conditions
def Check(c, b):
   
    # Storing frequencies
    m = {}
    for i in range(len(b)):
        if b[i] in m:
            m[b[i]] += 1
        else:
            m[b[i]] = 1
    v = []
    for key,value in m.items():
        v.append(value)
 
    # Function Call for backtracking
    check = getans(0, v, c)
    if (check):
        print("YES")
    else:
        print("NO")
 
# Driver Code
if __name__ == '__main__':
   
    # Given Input
    c = [1, 3, 3]
    b = [2, 2, 2, 2, 4, 4, 4]
 
    # Function Call
    Check(c, b)
     
    # This code is contributed by ipg2016107.


C#
// C# program for above approach
using System;
using System.Collections.Generic;
class GFG
{
   
    // A boolean function that returns true if it's possible
    // to completely fill the container else return false
    static bool getans(int i, List v, int[] q)
    {
        // Base Case
        if (i == q.Length)
            return true;
 
        // Backtracking
        for (int j = 0; j < v.Count; j++) {
            if (v[j] >= q[i]) {
                v[j] -= q[i];
                if (getans(i + 1, v, q)) {
                    return true;
                }
                v[j] += q[i];
            }
        }
        return false;
    }
 
    // Function to check the conditions
    static void Check(int[] c, int[] b)
    {
 
        // Storing frequencies
        Dictionary m = new Dictionary();
        for (int i = 0; i < b.Length; i++)
            m[b[i]] = 0;
        for (int i = 0; i < b.Length; i++) {
            m[b[i]]++;
        }
        List v = new List();
        foreach(KeyValuePair i in m)
        {
            v.Add(i.Value);
        }
 
        // Function Call for backtracking
        bool check = getans(0, v, c);
        if (check)
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
 
    // Driver Code
    public static void Main()
    {
       
        // Given Input
        int[] c = { 1, 3, 3 };
        int[] b = { 2, 2, 2, 2, 4, 4, 4 };
 
        // Function Call
        Check(c, b);
    }
}
 
// This code is contributed by ukasp.


Javascript



输出:
YES

时间复杂度: O(m^n),其中 n 是 arr[ ] C 的大小,m 是 arr[ ] B 的大小。辅助空间: O(m)