You are given an array of binary strings strs and two integers m and n.
Return the size of the largest subset of strs such that there are at most m 0's and n 1's in the subset.
A set x is a subset of a set y if all elements of x are also elements of y.
Example 1:
Input: strs = ["10","0001","111001","1","0"], m = 5, n = 3
Output: 4
Explanation: The largest subset with at most 5 0's and 3 1's is {"10", "0001", "1", "0"}, so the answer is 4.
Other valid but smaller subsets include {"0001", "1"} and {"10", "1", "0"}.
{"111001"} is an invalid subset because it contains 4 1's, greater than the maximum of 3.
Example 2:
Input: strs = ["10","0","1"], m = 1, n = 1
Output: 2
Explanation: The largest subset is {"0", "1"}, so the answer is 2.
Constraints:
1 <= strs.length <= 6001 <= strs[i].length <= 100strs[i] consists only of digits '0' and '1'.1 <= m, n <= 100This problem can be approached using a dynamic programming strategy similar to the knapsack problem. For each binary string, calculate the number of 0's and 1's it contains. Then, use a DP table to keep track of the maximum subset size you can achieve with a given count of 0's and 1's. We'll fill this table by iterating over each string and attempting to include it in our subset if the current capacity allows. Update the DP table in a reverse manner to avoid overwriting results prematurely.
In this C solution, we maintain a 2D array dp where dp[i][j] represents the maximum number of strings that can be formed with at most i zeros and j ones. For each binary string, we count zeros and ones, then update the DP table from back to front by considering if we include the current string in our subset. This ensures no overwriting of states that are yet to be calculated for the current iteration.
C++
Java
Python
C#
JavaScript
Time Complexity: O(strsSize * m * n) where strsSize is the number of binary strings.
Space Complexity: O(m * n) for storing the DP table.
The problem can also be tackled using a recursive function with memoization to store already computed results and avoid redundant calculations. This approach uses recursion to consider two choices for each string - either include it in the subset or not, based on the available capacity for zeros and ones. By storing intermediate results, we can significantly reduce the number of recursive calls needed, thus optimizing the process.
This C implementation uses a recursive helper function with memoization. The function considers the inclusion and exclusion of each string in the subset while respecting the limits on zeros and ones. Using a 3-dimensional array, memoized results prevent duplicate work and improve efficiency.
C++
Java
Python
C#
JavaScript
Time Complexity: O(strsSize * m * n) due to memoization.
Space Complexity: O(strsSize * m * n) for the memoization table.
| Approach | Complexity |
|---|---|
| Dynamic Programming Approach | Time Complexity: O(strsSize * m * n) where strsSize is the number of binary strings. |
| Memoized Recursive Approach | Time Complexity: O(strsSize * m * n) due to memoization. |
Set Matrix Zeroes - In-place - Leetcode 73 • NeetCode • 153,689 views views
Watch 9 more video solutions →Practice Ones and Zeroes with our built-in code editor and test cases.
Practice on FleetCode