Below is the implementation of the ones and zeroes:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899/** * Find the maximum size of the subset with at most m 0's and n 1's. * 3D Dynamic Programming approach. * * @param {string[]} strs - Array of binary strings * @param {number} m - Maximum number of 0's allowed * @param {number} n - Maximum number of 1's allowed * @return {number} - Maximum size of the subset */function findMaxForm(strs, m, n) { const len = strs.length; // Count the number of 0's and 1's in each string const zeros = new Array(len); const ones = new Array(len); for (let i = 0; i < len; i++) { zeros[i] = 0; ones[i] = 0; for (let j = 0; j < strs[i].length; j++) { if (strs[i][j] === '0') { zeros[i]++; } else { ones[i]++; } } } // Initialize 3D DP array const dp = Array(len + 1).fill().map(() => Array(m + 1).fill().map(() => Array(n + 1).fill(0) ) ); for (let i = 1; i <= len; i++) { const zero = zeros[i - 1]; const one = ones[i - 1]; for (let j = 0; j <= m; j++) { for (let k = 0; k <= n; k++) { // Exclude the string dp[i][j][k] = dp[i - 1][j][k]; // Include the string if we have enough capacity if (j >= zero && k >= one) { dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j - zero][k - one] + 1); } } } } return dp[len][m][n];} /** * Find the maximum size of the subset with at most m 0's and n 1's. * 2D Dynamic Programming approach (Space Optimization). * * @param {string[]} strs - Array of binary strings * @param {number} m - Maximum number of 0's allowed * @param {number} n - Maximum number of 1's allowed * @return {number} - Maximum size of the subset */function findMaxFormOptimized(strs, m, n) { // Initialize 2D DP array const dp = Array(m + 1).fill().map(() => Array(n + 1).fill(0)); for (const str of strs) { // Count the number of 0's and 1's in the current string let zero = 0; let one = 0; for (let i = 0; i < str.length; i++) { if (str[i] === '0') { zero++; } else { one++; } } // Update DP array in reverse order for (let j = m; j >= zero; j--) { for (let k = n; k >= one; k--) { dp[j][k] = Math.max(dp[j][k], dp[j - zero][k - one] + 1); } } } return dp[m][n];} // Test casesconsole.log(findMaxForm(["10", "0001", "111001", "1", "0"], 5, 3)); // 4console.log(findMaxForm(["10", "0", "1"], 1, 1)); // 2 console.log(findMaxFormOptimized(["10", "0001", "111001", "1", "0"], 5, 3)); // 4console.log(findMaxFormOptimized(["10", "0", "1"], 1, 1)); // 2
Let's break down the implementation:
Implement the ones and zeroes solution in different programming languages.
Below is the implementation of the ones and zeroes in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899/** * Find the maximum size of the subset with at most m 0's and n 1's. * 3D Dynamic Programming approach. * * @param {string[]} strs - Array of binary strings * @param {number} m - Maximum number of 0's allowed * @param {number} n - Maximum number of 1's allowed * @return {number} - Maximum size of the subset */function findMaxForm(strs, m, n) { const len = strs.length; // Count the number of 0's and 1's in each string const zeros = new Array(len); const ones = new Array(len); for (let i = 0; i < len; i++) { zeros[i] = 0; ones[i] = 0; for (let j = 0; j < strs[i].length; j++) { if (strs[i][j] === '0') { zeros[i]++; } else { ones[i]++; } } } // Initialize 3D DP array const dp = Array(len + 1).fill().map(() => Array(m + 1).fill().map(() => Array(n + 1).fill(0) ) ); for (let i = 1; i <= len; i++) { const zero = zeros[i - 1]; const one = ones[i - 1]; for (let j = 0; j <= m; j++) { for (let k = 0; k <= n; k++) { // Exclude the string dp[i][j][k] = dp[i - 1][j][k]; // Include the string if we have enough capacity if (j >= zero && k >= one) { dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j - zero][k - one] + 1); } } } } return dp[len][m][n];} /** * Find the maximum size of the subset with at most m 0's and n 1's. * 2D Dynamic Programming approach (Space Optimization). * * @param {string[]} strs - Array of binary strings * @param {number} m - Maximum number of 0's allowed * @param {number} n - Maximum number of 1's allowed * @return {number} - Maximum size of the subset */function findMaxFormOptimized(strs, m, n) { // Initialize 2D DP array const dp = Array(m + 1).fill().map(() => Array(n + 1).fill(0)); for (const str of strs) { // Count the number of 0's and 1's in the current string let zero = 0; let one = 0; for (let i = 0; i < str.length; i++) { if (str[i] === '0') { zero++; } else { one++; } } // Update DP array in reverse order for (let j = m; j >= zero; j--) { for (let k = n; k >= one; k--) { dp[j][k] = Math.max(dp[j][k], dp[j - zero][k - one] + 1); } } } return dp[m][n];} // Test casesconsole.log(findMaxForm(["10", "0001", "111001", "1", "0"], 5, 3)); // 4console.log(findMaxForm(["10", "0", "1"], 1, 1)); // 2 console.log(findMaxFormOptimized(["10", "0001", "111001", "1", "0"], 5, 3)); // 4console.log(findMaxFormOptimized(["10", "0", "1"], 1, 1)); // 2
First, count the number of 0's and 1's in each binary string.
Initialize a DP array to store the maximum size of the subset for each combination of strings, 0's, and 1's.
For each string, update the DP array by considering two options: include the string or exclude it.
Optimize the space complexity by using a 2D DP array instead of a 3D DP array, updating it in reverse order to avoid using updated values in the same iteration.
Return the maximum size of the subset that satisfies the constraints.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the ones and zeroes problem using a different approach than shown above.
Handle the case where there is no valid subset that satisfies the constraints (return 0).
Handle the case where all strings can be included in the subset.
Handle the case where there is an empty string in the array (it has 0 zeros and 0 ones).
Below is the implementation of the ones and zeroes:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899/** * Find the maximum size of the subset with at most m 0's and n 1's. * 3D Dynamic Programming approach. * * @param {string[]} strs - Array of binary strings * @param {number} m - Maximum number of 0's allowed * @param {number} n - Maximum number of 1's allowed * @return {number} - Maximum size of the subset */function findMaxForm(strs, m, n) { const len = strs.length; // Count the number of 0's and 1's in each string const zeros = new Array(len); const ones = new Array(len); for (let i = 0; i < len; i++) { zeros[i] = 0; ones[i] = 0; for (let j = 0; j < strs[i].length; j++) { if (strs[i][j] === '0') { zeros[i]++; } else { ones[i]++; } } } // Initialize 3D DP array const dp = Array(len + 1).fill().map(() => Array(m + 1).fill().map(() => Array(n + 1).fill(0) ) ); for (let i = 1; i <= len; i++) { const zero = zeros[i - 1]; const one = ones[i - 1]; for (let j = 0; j <= m; j++) { for (let k = 0; k <= n; k++) { // Exclude the string dp[i][j][k] = dp[i - 1][j][k]; // Include the string if we have enough capacity if (j >= zero && k >= one) { dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j - zero][k - one] + 1); } } } } return dp[len][m][n];} /** * Find the maximum size of the subset with at most m 0's and n 1's. * 2D Dynamic Programming approach (Space Optimization). * * @param {string[]} strs - Array of binary strings * @param {number} m - Maximum number of 0's allowed * @param {number} n - Maximum number of 1's allowed * @return {number} - Maximum size of the subset */function findMaxFormOptimized(strs, m, n) { // Initialize 2D DP array const dp = Array(m + 1).fill().map(() => Array(n + 1).fill(0)); for (const str of strs) { // Count the number of 0's and 1's in the current string let zero = 0; let one = 0; for (let i = 0; i < str.length; i++) { if (str[i] === '0') { zero++; } else { one++; } } // Update DP array in reverse order for (let j = m; j >= zero; j--) { for (let k = n; k >= one; k--) { dp[j][k] = Math.max(dp[j][k], dp[j - zero][k - one] + 1); } } } return dp[m][n];} // Test casesconsole.log(findMaxForm(["10", "0001", "111001", "1", "0"], 5, 3)); // 4console.log(findMaxForm(["10", "0", "1"], 1, 1)); // 2 console.log(findMaxFormOptimized(["10", "0001", "111001", "1", "0"], 5, 3)); // 4console.log(findMaxFormOptimized(["10", "0", "1"], 1, 1)); // 2
Let's break down the implementation:
Implement the ones and zeroes solution in different programming languages.
Below is the implementation of the ones and zeroes in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899/** * Find the maximum size of the subset with at most m 0's and n 1's. * 3D Dynamic Programming approach. * * @param {string[]} strs - Array of binary strings * @param {number} m - Maximum number of 0's allowed * @param {number} n - Maximum number of 1's allowed * @return {number} - Maximum size of the subset */function findMaxForm(strs, m, n) { const len = strs.length; // Count the number of 0's and 1's in each string const zeros = new Array(len); const ones = new Array(len); for (let i = 0; i < len; i++) { zeros[i] = 0; ones[i] = 0; for (let j = 0; j < strs[i].length; j++) { if (strs[i][j] === '0') { zeros[i]++; } else { ones[i]++; } } } // Initialize 3D DP array const dp = Array(len + 1).fill().map(() => Array(m + 1).fill().map(() => Array(n + 1).fill(0) ) ); for (let i = 1; i <= len; i++) { const zero = zeros[i - 1]; const one = ones[i - 1]; for (let j = 0; j <= m; j++) { for (let k = 0; k <= n; k++) { // Exclude the string dp[i][j][k] = dp[i - 1][j][k]; // Include the string if we have enough capacity if (j >= zero && k >= one) { dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j - zero][k - one] + 1); } } } } return dp[len][m][n];} /** * Find the maximum size of the subset with at most m 0's and n 1's. * 2D Dynamic Programming approach (Space Optimization). * * @param {string[]} strs - Array of binary strings * @param {number} m - Maximum number of 0's allowed * @param {number} n - Maximum number of 1's allowed * @return {number} - Maximum size of the subset */function findMaxFormOptimized(strs, m, n) { // Initialize 2D DP array const dp = Array(m + 1).fill().map(() => Array(n + 1).fill(0)); for (const str of strs) { // Count the number of 0's and 1's in the current string let zero = 0; let one = 0; for (let i = 0; i < str.length; i++) { if (str[i] === '0') { zero++; } else { one++; } } // Update DP array in reverse order for (let j = m; j >= zero; j--) { for (let k = n; k >= one; k--) { dp[j][k] = Math.max(dp[j][k], dp[j - zero][k - one] + 1); } } } return dp[m][n];} // Test casesconsole.log(findMaxForm(["10", "0001", "111001", "1", "0"], 5, 3)); // 4console.log(findMaxForm(["10", "0", "1"], 1, 1)); // 2 console.log(findMaxFormOptimized(["10", "0001", "111001", "1", "0"], 5, 3)); // 4console.log(findMaxFormOptimized(["10", "0", "1"], 1, 1)); // 2
First, count the number of 0's and 1's in each binary string.
Initialize a DP array to store the maximum size of the subset for each combination of strings, 0's, and 1's.
For each string, update the DP array by considering two options: include the string or exclude it.
Optimize the space complexity by using a 2D DP array instead of a 3D DP array, updating it in reverse order to avoid using updated values in the same iteration.
Return the maximum size of the subset that satisfies the constraints.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the ones and zeroes problem using a different approach than shown above.
Handle the case where there is no valid subset that satisfies the constraints (return 0).
Handle the case where all strings can be included in the subset.
Handle the case where there is an empty string in the array (it has 0 zeros and 0 ones).