Ones and Zeroes
Ones and Zeroes is LeetCode 474 (Medium). You get an array strs of binary strings and two limits, m and n. Pick as many strings as you can so that, all together, they contain at most m zeros and at most n ones. Return how many strings you picked.
- Each string is used at most once.
- The letters inside a string do not matter, only how many 0s and how many 1s it has.
- With up to 600 strings, trying every subset is out of the question, but
mandnare at most 100, so a table over the two limits is small.
- 1 <= strs.length <= 600
- 1 <= strs[i].length <= 100
- strs[i] consists only of digits '0' and '1'.
- 1 <= m, n <= 100
Intuition
Turn each string into a price with two parts: its count of 0s and its count of 1s. Now the question is the 0/1 knapsack with two wallets instead of one, and every item is worth exactly 1.
Keep a grid where dp[i][j] is the most strings you can afford with i zeros and j ones to spend. When a new string arrives, each cell either ignores it or buys it, and buying it leaves the budget (i − zeros, j − ones), whose best count is already in the grid. Taking the larger of the two keeps every cell correct after every string.
Choose a subset, each item at most once, while two separate totals stay under two separate limits. That is a knapsack whose capacity is a pair of numbers, so the table gets one dimension per limit.
Approach
Before reading on, ask what two numbers describe what is left of your budget after some choices. Write the best count for that budget in terms of whether you take one string. Aim for O(L · m · n), where L is the number of strings.
Count each string once
For every string, count its zeros; its ones are the length minus that. From here on the string is just the pair (zeros, ones), because the order of its characters never affects the budget.
A grid over both budgets
Make dp with m + 1 rows and n + 1 columns, all 0. Before any string is seen, no budget can buy anything, so 0 is correct for every cell.
Buy or skip, from the largest budget down
For each string, visit i from m down to zeros and j from n down to ones, and set dp[i][j] to the larger of itself and dp[i − zeros][j − ones] + 1. Going downwards means the smaller cell you read has not been updated for this string yet, so the string is counted at most once.
Read the full budget
After the last string, dp[m][n] is the most strings that fit in m zeros and n ones. A string with more zeros than m or more ones than n never enters the loops, so it is skipped automatically.
Code in Python, C++ and Java
Common pitfalls
Looping the budgets upwards
for i in range(zeros, m + 1):
for j in range(ones, n + 1):for i in range(m, zeros - 1, -1):
for j in range(n, ones - 1, -1):Upwards, a cell can read a smaller cell that this same string already updated, so one string is bought twice. For strs = ["0"], m = 2, n = 1 the upward loop returns 2, but there is only one string.
Picking short strings first
sort strs by length, take while both budgets last
fill the two-budget grid
Length ignores which budget a string drains. For strs = ["00", "10", "01"], m = 2, n = 3 all three have length 2; taking "00" first spends both zeros and nothing else fits, giving 1, while "10" and "01" together give 2.
Edge cases
The zeros loop starts at 2 and stops before reaching 3, so the string never touches the grid. No special check is needed.
Complexity
Ones and Zeroes FAQ
Can the grid have a third dimension for the string index?
Yes: dp[k][i][j] for the first k strings, which lets you loop the budgets in any direction. It uses about 600 × 101 × 101 cells, roughly 6 million, so the two-dimensional grid is preferred.
Which problems use the same trick with one budget?
Partition Equal Subset Sum asks whether a subset hits an exact sum with one budget, and the 0/1 knapsack maximises value under one weight limit. Adding a limit adds a dimension to the table.