474. 一和零
二维费用背包问题
class Solution {
public:
int findMaxForm(vector<string>& strs, int m, int n) {
int l = strs.size();
int dp[110][110] = {0};
//dp[i][j]表示有i个0,j个1的子集长度
for (int i = 0; i < l; i++) {
//遍历字符数组
int one = 0, zero = 0;
int len = strs[i].size();
for (int j = 0; j < len; j++) {
if (strs[i][j] == '1')
one++;
else
zero++;
}
//算第i个字符串有多少0,多少1
for (int j = m; j >= zero; j--)
for (int k = n; k >= one; k--)
dp[j][k] = max(dp[j][k], dp[j - zero][k - one] + 1);
//更新dp数组,要么维持前一个字符串的结果,要么取该字符串
}
return dp[m][n];
}
};