Skip to content

Latest commit

 

History

History
110 lines (90 loc) · 4.59 KB

README.md

File metadata and controls

110 lines (90 loc) · 4.59 KB

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 <= 600
  • 1 <= strs[i].length <= 100
  • strs[i] consists only of digits '0' and '1'.
  • 1 <= m, n <= 100

Companies: Uber, Google, Adobe, Amazon, Apple

Related Topics:
Array, String, Dynamic Programming

Similar Questions:

Solution 1. DP

Note: this is a 0-1 Knapsack problem.

Let dp[i + 1][j][k] be the answer of subproblem if we only use the first i + 1 strings (strs[0] to strs[i]) given m = j, n = k.

dp[i + 1][j][k] = max(
                        dp[i][j][k],                          // If we don't use strs[i]
                        1 + dp[i][j - zero[i]][k - one[i]]    // If we use strs[i] 
                     )

where zero[i] and one[i] are the counts of zeros and ones in strs[i] respectively.

// OJ: https://leetcode.com/problems/ones-and-zeroes/
// Author: github.com/lzl124631x
// Time: O(MNS)
// Space: O(MNS)
class Solution {
public:
    int findMaxForm(vector<string>& strs, int m, int n) {
        int S = strs.size();
        vector<vector<vector<int>>> dp(S + 1, vector<vector<int>>(m + 1, vector<int>(n + 1)));
        for (int i = 0; i < S; ++i) {
            int zero = count(strs[i].begin(), strs[i].end(), '0'), one = strs[i].size() - zero;
            for (int j = 0; j <= m; ++j) {
                for (int k = 0; k <= n; ++k) {
                    dp[i + 1][j][k] = max(dp[i][j][k], j >= zero && k >= one ? 1 + dp[i][j - zero][k - one] : 0);
                }
            }
        }
        return dp[S][m][n];
    }
};

Solution 2. DP with Space Optimization

// OJ: https://leetcode.com/problems/ones-and-zeroes/
// Author: github.com/lzl124631x
// Time: O(MNS)
// Space: O(MN)
class Solution {
public:
    int findMaxForm(vector<string>& strs, int m, int n) {
        int S = strs.size();
        vector<vector<int>> dp(m + 1, vector<int>(n + 1));
        for (int i = 0; i < S; ++i) {
            int zero = count(strs[i].begin(), strs[i].end(), '0'), one = strs[i].size() - zero;
            for (int j = m; j >= zero; --j) {
                for (int k = n; k >= one; --k) {
                    dp[j][k] = max(dp[j][k], 1 + dp[j - zero][k - one]);
                }
            }
        }
        return dp[m][n];
    }
};