474. Ones and Zeroes
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.
思路
这道题本质上还是01背包问题,strs数组中的每一个元素是物品,只是这里物品的重量有两个维度:“0”的数量和“1”的数量。
所以这里dp数组要设为二维数组,dp[i][j]表示在strs中最多有i个0和j个1的最大子集数。
递推公式dp[i][j] = max( dp[i][j], dp[i-zeroNum][j-oneNum]+1 )。
需要注意的是这里是+1而不是+value[i][j],因为dp数组表示的是子集数,将当前子集放入,子集数就+1。
AC代码
class Solution {
public:
int findMaxForm(vector<string>& strs, int m, int n) {
vector<vector<int>> dp(m+1,vector<int>(n+1,0));
for(string str:strs){
int zeroNum=0,oneNum=0;
for(char c:str){
if(c=='0')zeroNum++;
else oneNum++;
}
for(int i=m;i>=zeroNum;i--){
for(int j=n;j>=oneNum;j--){
dp[i][j]=max(dp[i][j],dp[i-zeroNum][j-oneNum]+1);
}
}
}
return dp[m][n];
}
};