Welcome to Subscribe On Youtube
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:
- The given numbers of
0s
and1s
will both not exceed100
- 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:
Google
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[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/
// 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];
}
};
-
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[0], ones = zerosOnes[1]; 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[2]; int length = str.length(); for (int i = 0; i < length; i++) { char c = str.charAt(i); count[c - '0']++; } return count; } } ############ class Solution { public int findMaxForm(String[] strs, int m, int n) { int[][] dp = new int[m + 1][n + 1]; for (String s : strs) { int[] t = count(s); for (int i = m; i >= t[0]; --i) { for (int j = n; j >= t[1]; --j) { dp[i][j] = Math.max(dp[i][j], dp[i - t[0]][j - t[1]] + 1); } } } return dp[m][n]; } private int[] count(String s) { int n0 = 0; for (char c : s.toCharArray()) { if (c == '0') { ++n0; } } return new int[] {n0, s.length() - n0}; } }
-
// 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: def findMaxForm(self, strs: List[str], m: int, n: int) -> int: dp = [[0] * (n + 1) for _ in range(m + 1)] t = [(s.count('0'), s.count('1')) for s in strs] for k in range(len(strs)): n0, n1 = t[k] for i in range(m, n0 - 1, -1): for j in range(n, n1 - 1, -1): dp[i][j] = max(dp[i][j], dp[i - n0][j - n1] + 1) return dp[-1][-1] ############ class Solution(object): def findMaxForm(self, strs, m, n): """ :type strs: List[str] :type m: int :type n: int :rtype: int """ dp = [[0] * (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]
-
func findMaxForm(strs []string, m int, n int) int { dp := make([][]int, m+1) for i := 0; i < m+1; i++ { dp[i] = make([]int, n+1) } for _, s := range strs { t := count(s) for i := m; i >= t[0]; i-- { for j := n; j >= t[1]; j-- { dp[i][j] = max(dp[i][j], dp[i-t[0]][j-t[1]]+1) } } } return dp[m][n] } func count(s string) []int { n0 := 0 for i := 0; i < len(s); i++ { if s[i] == '0' { n0++ } } return []int{n0, len(s) - n0} } func max(a, b int) int { if a > b { return a } return b }