Ones and Zeroes
1 approach, code in every language
Problem description
You are given an array of binary strings and two integers m and n. Think of m as a budget of zeros and n as a budget of ones that you are allowed to spend.
You want to select a subset of the given strings such that the combined number of zeros across the chosen strings is at most m and the combined number of ones is at most n.
Return the size of the largest such subset that respects both budgets.
Example 1
Input: strs = ["10","0001","111001","1","0"], m = 5, n = 3
Output: 4
The largest subset using at most 5 zeros and 3 ones is {"10","0001","1","0"}, which contains 4 strings.
Example 2
Input: strs = ["10","0","1"], m = 1, n = 1
Output: 2
With one zero and one one available, the best choice is {"0","1"}, giving a subset of size 2.
Constraints
1 <= strs.length <= 6001 <= strs[i].length <= 100strs[i] consists only of the characters '0' and '1'1 <= m, n <= 100
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Stamping the Most Part Codes is our own problem on the technique behind Ones and Zeroes: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.
1,194 problems · 15,062 graded tests · free