Formatted question description: https://leetcode.ca/all/474.html

# 474. Ones and Zeroes (Medium)

In the computer world, use restricted resource you have to generate maximum benefit is what we always want to pursue.

For now, suppose you are a dominator of m 0s and n 1s respectively. On the other hand, there is an array with strings consisting of only 0s and 1s.

Now your task is to find the maximum number of strings that you can form with given m 0s and n 1s. Each 0 and 1 can be used at most once.

Note:

1. The given numbers of 0s and 1s will both not exceed 100
2. The size of given string array won't exceed 600.

Example 1:

Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
Output: 4

Explanation: This are totally 4 strings can be formed by the using of 5 0s and 3 1s, which are “10,”0001”,”1”,”0”


Example 2:

Input: Array = {"10", "0", "1"}, m = 1, n = 1
Output: 2

Explanation: You could form "10", but then you'd have nothing left. Better form "0" and "1".


Companies:

Related Topics:
Dynamic Programming

## Solution 1. DP

Let dp[i + 1][j][k] be the answer of subproblem if we only use the first i + 1 strings (strs 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/
// 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/
// 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 >= 0; --j) {
for (int k = n; k >= 0; --k) {
dp[j][k] = max(dp[j][k], j >= zero && k >= one ? 1 + dp[j - zero][k - one] : 0);
}
}
}
return dp[m][n];
}
};


Java

• class Solution {
public int findMaxForm(String[] strs, int m, int n) {
int length = strs.length;
int[][][] dp = new int[length + 1][m + 1][n + 1];
for (int i = 1; i <= length; i++) {
String str = strs[i - 1];
int[] zerosOnes = countZerosOnes(str);
int zeros = zerosOnes, ones = zerosOnes;
for (int j = 0; j <= m; j++) {
for (int k = 0; k <= n; k++) {
if (j < zeros || k < ones)
dp[i][j][k] = dp[i - 1][j][k];
else
dp[i][j][k] = Math.max(dp[i - 1][j][k], dp[i - 1][j - zeros][k - ones] + 1);
}
}
}
return dp[length][m][n];
}

public int[] countZerosOnes(String str) {
int[] count = new int;
int length = str.length();
for (int i = 0; i < length; i++) {
char c = str.charAt(i);
count[c - '0']++;
}
return count;
}
}

• // OJ: https://leetcode.com/problems/ones-and-zeroes/
// 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];
}
};

• class Solution(object):
def findMaxForm(self, strs, m, n):
"""
:type strs: List[str]
:type m: int
:type n: int
:rtype: int
"""
dp = [ * (n + 1) for _ in range(0, m + 1)]
for dj, dk in [(s.count("0"), s.count("1")) for s in strs]:
for j in reversed(range(0, m + 1)):
for k in reversed(range(0, n + 1)):
if j - dj >= 0 and k - dk >= 0:
dp[j][k] = max(dp[j][k], dp[j - dj][k - dk] + 1)
return dp[-1][-1]