There are 2 main approaches to solve this problem:
The key insight for solving this problem is to recognize it as a variation of the 0/1 Knapsack problem with two constraints: the number of 0's and the number of 1's.
Let's define dp[i][j][k]
as the maximum size of the subset we can form using the first i
strings, with at most j
0's and k
1's.
For each string, we have two options:
If we include the string, we need to ensure that we have enough capacity for its 0's and 1's. The recurrence relation is:
dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j-zeros][k-ones] + 1)
if j >= zeros
and k >= ones
where zeros
and ones
are the number of 0's and 1's in the i
-th string.
If we exclude the string, the recurrence relation is:
dp[i][j][k] = dp[i-1][j][k]
The final answer is dp[n][m][n]
, where n
is the number of strings.
Where n is the number of strings and m is the maximum number of 0's allowed. We need to compute dp[i][j][k] for each i from 0 to n, each j from 0 to m, and each k from 0 to n.
We use a 3D DP array of size (n+1) x (m+1) x (n+1).
We can optimize the space complexity of the previous approach by observing that to calculate dp[i][j][k]
, we only need the values from dp[i-1][j][k]
.
Instead of using a 3D array, we can use a 2D array dp[j][k]
to represent the maximum size of the subset we can form with at most j
0's and k
1's.
For each string, we update the DP array in reverse order to avoid using the updated values in the same iteration.
The recurrence relation becomes:
dp[j][k] = max(dp[j][k], dp[j-zeros][k-ones] + 1)
if j >= zeros
and k >= ones
This reduces the space complexity from O(n * m * n) to O(m * n).
Where n is the number of strings and m is the maximum number of 0's allowed. We need to compute dp[j][k] for each string, each j from zeros[i] to m, and each k from ones[i] to n.
We use a 2D DP array of size (m+1) x (n+1).
1234567891011121314151617181920212223242526function findMaxForm(strs, m, n): // Count the number of 0's and 1's in each string zeros = new array of size strs.length ones = new array of size strs.length for i from 0 to strs.length - 1: for char in strs[i]: if char == '0': zeros[i]++ else: ones[i]++ // Initialize 3D DP array dp = new 3D array of size (strs.length + 1) x (m + 1) x (n + 1), initialized with 0 for i from 1 to strs.length: for j from 0 to m: for k from 0 to n: // Exclude the string dp[i][j][k] = dp[i - 1][j][k] // Include the string if we have enough capacity if j >= zeros[i - 1] and k >= ones[i - 1]: dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j - zeros[i - 1]][k - ones[i - 1]] + 1) return dp[strs.length][m][n]
Understand different approaches to solve the ones and zeroes problem and analyze their efficiency.
The key insight for solving this problem is to recognize it as a variation of the 0/1 Knapsack problem with two constraints: the number of 0's and the number of 1's.
Let's define dp[i][j][k]
as the maximum size of the subset we can form using the first i
strings, with at most j
0's and k
1's.
For each string, we have two options:
If we include the string, we need to ensure that we have enough capacity for its 0's and 1's. The recurrence relation is:
dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j-zeros][k-ones] + 1)
if j >= zeros
and k >= ones
where zeros
and ones
are the number of 0's and 1's in the i
-th string.
If we exclude the string, the recurrence relation is:
dp[i][j][k] = dp[i-1][j][k]
The final answer is dp[n][m][n]
, where n
is the number of strings.
We can optimize the space complexity of the previous approach by observing that to calculate dp[i][j][k]
, we only need the values from dp[i-1][j][k]
.
Instead of using a 3D array, we can use a 2D array dp[j][k]
to represent the maximum size of the subset we can form with at most j
0's and k
1's.
For each string, we update the DP array in reverse order to avoid using the updated values in the same iteration.
The recurrence relation becomes:
dp[j][k] = max(dp[j][k], dp[j-zeros][k-ones] + 1)
if j >= zeros
and k >= ones
This reduces the space complexity from O(n * m * n) to O(m * n).
Where n is the number of strings and m is the maximum number of 0's allowed. We need to compute dp[i][j][k] for each i from 0 to n, each j from 0 to m, and each k from 0 to n.
We use a 3D DP array of size (n+1) x (m+1) x (n+1).
Where n is the number of strings and m is the maximum number of 0's allowed. We need to compute dp[j][k] for each string, each j from zeros[i] to m, and each k from ones[i] to n.
We use a 2D DP array of size (m+1) x (n+1).
1234567891011121314151617181920212223242526function findMaxForm(strs, m, n): // Count the number of 0's and 1's in each string zeros = new array of size strs.length ones = new array of size strs.length for i from 0 to strs.length - 1: for char in strs[i]: if char == '0': zeros[i]++ else: ones[i]++ // Initialize 3D DP array dp = new 3D array of size (strs.length + 1) x (m + 1) x (n + 1), initialized with 0 for i from 1 to strs.length: for j from 0 to m: for k from 0 to n: // Exclude the string dp[i][j][k] = dp[i - 1][j][k] // Include the string if we have enough capacity if j >= zeros[i - 1] and k >= ones[i - 1]: dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j - zeros[i - 1]][k - ones[i - 1]] + 1) return dp[strs.length][m][n]
123456789101112131415161718192021function findMaxForm(strs, m, n): // Count the number of 0's and 1's in each string zeros = new array of size strs.length ones = new array of size strs.length for i from 0 to strs.length - 1: for char in strs[i]: if char == '0': zeros[i]++ else: ones[i]++ // Initialize 2D DP array dp = new 2D array of size (m + 1) x (n + 1), initialized with 0 for i from 0 to strs.length - 1: for j from m down to zeros[i]: for k from n down to ones[i]: dp[j][k] = max(dp[j][k], dp[j - zeros[i]][k - ones[i]] + 1) return dp[m][n]
There are 2 main approaches to solve this problem:
The key insight for solving this problem is to recognize it as a variation of the 0/1 Knapsack problem with two constraints: the number of 0's and the number of 1's.
Let's define dp[i][j][k]
as the maximum size of the subset we can form using the first i
strings, with at most j
0's and k
1's.
For each string, we have two options:
If we include the string, we need to ensure that we have enough capacity for its 0's and 1's. The recurrence relation is:
dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j-zeros][k-ones] + 1)
if j >= zeros
and k >= ones
where zeros
and ones
are the number of 0's and 1's in the i
-th string.
If we exclude the string, the recurrence relation is:
dp[i][j][k] = dp[i-1][j][k]
The final answer is dp[n][m][n]
, where n
is the number of strings.
Where n is the number of strings and m is the maximum number of 0's allowed. We need to compute dp[i][j][k] for each i from 0 to n, each j from 0 to m, and each k from 0 to n.
We use a 3D DP array of size (n+1) x (m+1) x (n+1).
We can optimize the space complexity of the previous approach by observing that to calculate dp[i][j][k]
, we only need the values from dp[i-1][j][k]
.
Instead of using a 3D array, we can use a 2D array dp[j][k]
to represent the maximum size of the subset we can form with at most j
0's and k
1's.
For each string, we update the DP array in reverse order to avoid using the updated values in the same iteration.
The recurrence relation becomes:
dp[j][k] = max(dp[j][k], dp[j-zeros][k-ones] + 1)
if j >= zeros
and k >= ones
This reduces the space complexity from O(n * m * n) to O(m * n).
Where n is the number of strings and m is the maximum number of 0's allowed. We need to compute dp[j][k] for each string, each j from zeros[i] to m, and each k from ones[i] to n.
We use a 2D DP array of size (m+1) x (n+1).
1234567891011121314151617181920212223242526function findMaxForm(strs, m, n): // Count the number of 0's and 1's in each string zeros = new array of size strs.length ones = new array of size strs.length for i from 0 to strs.length - 1: for char in strs[i]: if char == '0': zeros[i]++ else: ones[i]++ // Initialize 3D DP array dp = new 3D array of size (strs.length + 1) x (m + 1) x (n + 1), initialized with 0 for i from 1 to strs.length: for j from 0 to m: for k from 0 to n: // Exclude the string dp[i][j][k] = dp[i - 1][j][k] // Include the string if we have enough capacity if j >= zeros[i - 1] and k >= ones[i - 1]: dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j - zeros[i - 1]][k - ones[i - 1]] + 1) return dp[strs.length][m][n]
Understand different approaches to solve the ones and zeroes problem and analyze their efficiency.
The key insight for solving this problem is to recognize it as a variation of the 0/1 Knapsack problem with two constraints: the number of 0's and the number of 1's.
Let's define dp[i][j][k]
as the maximum size of the subset we can form using the first i
strings, with at most j
0's and k
1's.
For each string, we have two options:
If we include the string, we need to ensure that we have enough capacity for its 0's and 1's. The recurrence relation is:
dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j-zeros][k-ones] + 1)
if j >= zeros
and k >= ones
where zeros
and ones
are the number of 0's and 1's in the i
-th string.
If we exclude the string, the recurrence relation is:
dp[i][j][k] = dp[i-1][j][k]
The final answer is dp[n][m][n]
, where n
is the number of strings.
We can optimize the space complexity of the previous approach by observing that to calculate dp[i][j][k]
, we only need the values from dp[i-1][j][k]
.
Instead of using a 3D array, we can use a 2D array dp[j][k]
to represent the maximum size of the subset we can form with at most j
0's and k
1's.
For each string, we update the DP array in reverse order to avoid using the updated values in the same iteration.
The recurrence relation becomes:
dp[j][k] = max(dp[j][k], dp[j-zeros][k-ones] + 1)
if j >= zeros
and k >= ones
This reduces the space complexity from O(n * m * n) to O(m * n).
Where n is the number of strings and m is the maximum number of 0's allowed. We need to compute dp[i][j][k] for each i from 0 to n, each j from 0 to m, and each k from 0 to n.
We use a 3D DP array of size (n+1) x (m+1) x (n+1).
Where n is the number of strings and m is the maximum number of 0's allowed. We need to compute dp[j][k] for each string, each j from zeros[i] to m, and each k from ones[i] to n.
We use a 2D DP array of size (m+1) x (n+1).
1234567891011121314151617181920212223242526function findMaxForm(strs, m, n): // Count the number of 0's and 1's in each string zeros = new array of size strs.length ones = new array of size strs.length for i from 0 to strs.length - 1: for char in strs[i]: if char == '0': zeros[i]++ else: ones[i]++ // Initialize 3D DP array dp = new 3D array of size (strs.length + 1) x (m + 1) x (n + 1), initialized with 0 for i from 1 to strs.length: for j from 0 to m: for k from 0 to n: // Exclude the string dp[i][j][k] = dp[i - 1][j][k] // Include the string if we have enough capacity if j >= zeros[i - 1] and k >= ones[i - 1]: dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j - zeros[i - 1]][k - ones[i - 1]] + 1) return dp[strs.length][m][n]
123456789101112131415161718192021function findMaxForm(strs, m, n): // Count the number of 0's and 1's in each string zeros = new array of size strs.length ones = new array of size strs.length for i from 0 to strs.length - 1: for char in strs[i]: if char == '0': zeros[i]++ else: ones[i]++ // Initialize 2D DP array dp = new 2D array of size (m + 1) x (n + 1), initialized with 0 for i from 0 to strs.length - 1: for j from m down to zeros[i]: for k from n down to ones[i]: dp[j][k] = max(dp[j][k], dp[j - zeros[i]][k - ones[i]] + 1) return dp[m][n]