class Solution {
public:
int findMaxForm(vector<string>& strs, int m, int n) {
//declare dp array And init
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
//for loop
for (auto& string : strs) {
int x = 0; // the number of 0;
int y = 0; // the number of 1;
for (int i = 0; i < string.length(); i++) {
if (string[i] == '0') x++;
else y++;
}
for (int i = m; i >= x; i--) {
for (int j = n; j >= y; j--) {
dp[i][j] = max(dp[i][j], dp[i - x][j - y] + 1);
}
}
}
return dp[m][n];
}
};
LeetCode474.一和零
最新推荐文章于 2024-07-13 13:36:16 发布