📌  相关文章
📜  字符串数组中由A 0和B 1组成的最长子集的长度|套装2

📅  最后修改于: 2021-04-22 02:10:10             🧑  作者: Mango

给定一个数组arr [] ,该数组由N个二进制字符串以及两个整数AB组成,任务是 求出最多由A 0 s和B 1 s组成的最长子集的长度。

例子:

天真的方法:有关解决问题的最简单方法,请参阅本文的上一篇文章。
时间复杂度: O(2 N )
辅助空间: O(1)

动态编程方法:有关动态编程方法,请参阅本文的上一篇文章。
时间复杂度: O(N * A * B)
辅助空间: O(N * A * B)

空间优化的动态规划方法:可基于以下观察来优化上述方法中的空间复杂度:

  • 初始化一个2D数组dp [A] [B] ,其中dp [i] [j]表示最长i个子集的长度,该子集最多包含0个i数和1个j数。
  • 为了优化从3D表2D表的辅助空间,其想法是以相反的顺序遍历内部循环。
  • 这样可以确保每当dp [] []中的值更改时,在当前迭代中就不再需要该值。
  • 因此,递归关系如下所示:

请按照以下步骤解决问题:

  • 初始化一个2D数组,例如dp [A] [B] ,并将其所有条目初始化为0
  • 遍历给定的数组arr [] ,并对每个二进制字符串执行以下步骤:
    • 将当前字符串中的0和1计数分别存储在变量01中
    • 迭代在范围[A,零]使用同时迭代在范围[B,那些]使用变量j和更新到最大DPDP [i] [j]的值的变量i[I] [j]的(dp [i –零] [j –个] + 1)
  • 完成上述步骤后,打印dp [A] [B]的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the length of the
// longest subset of an array of strings
// with at most A 0s and B 1s
int MaxSubsetlength(vector arr,
                    int A, int B)
{
    // Initialize a 2D array with its
    // entries as 0
    int dp[A + 1][B + 1];
    memset(dp, 0, sizeof(dp));
 
    // Traverse the given array
    for (auto& str : arr) {
 
        // Store the count of 0s and 1s
        // in the current string
        int zeros = count(str.begin(),
                          str.end(), '0');
        int ones = count(str.begin(),
                         str.end(), '1');
 
        // Iterate in the range [A, zeros]
        for (int i = A; i >= zeros; i--)
 
            // Iterate in the range [B, ones]
            for (int j = B; j >= ones; j--)
 
                // Update the value of dp[i][j]
                dp[i][j] = max(
                    dp[i][j],
                    dp[i - zeros][j - ones] + 1);
    }
 
    // Print the result
    return dp[A][B];
}
 
// Driver Code
int main()
{
    vector arr
        = { "1", "0", "0001",
            "10", "111001" };
    int A = 5, B = 3;
    cout << MaxSubsetlength(arr, A, B);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to find the length of the
// longest subset of an array of strings
// with at most A 0s and B 1s
static int MaxSubsetlength(String arr[],
                           int A, int B)
{
     
    // Initialize a 2D array with its
    // entries as 0
    int dp[][] = new int[A + 1][B + 1];
 
    // Traverse the given array
    for(String str : arr)
    {
         
        // Store the count of 0s and 1s
        // in the current string
        int zeros = 0, ones = 0;
        for(char ch : str.toCharArray())
        {
            if (ch == '0')
                zeros++;
            else
                ones++;
        }
 
        // Iterate in the range [A, zeros]
        for(int i = A; i >= zeros; i--)
         
            // Iterate in the range [B, ones]
            for(int j = B; j >= ones; j--)
 
                // Update the value of dp[i][j]
                dp[i][j] = Math.max(
                    dp[i][j],
                    dp[i - zeros][j - ones] + 1);
    }
 
    // Print the result
    return dp[A][B];
}
 
// Driver Code
public static void main(String[] args)
{
    String arr[] = { "1", "0", "0001",
                     "10", "111001" };
    int A = 5, B = 3;
     
    System.out.println(MaxSubsetlength(arr, A, B));
}
}
 
// This code is contributed by Kingash


输出:
4

时间复杂度: O(N * A * B)
辅助空间: O(A * B)