diff --git a/474.cpp b/474.cpp new file mode 100644 index 0000000..2cd9b2a --- /dev/null +++ b/474.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int findMaxForm(vector& strs, int m, int n) { + // dp[i][j] = max number of strings we can form with i zeros and j ones + vector> dp(m + 1, vector(n + 1, 0)); + + for (const string& s : strs) { + int zeros = 0, ones = 0; + for (char c : s) { + if (c == '0') zeros++; + else ones++; + } + + // Reverse iterate to prevent reusing the same string + for (int i = m; i >= zeros; --i) { + for (int j = n; j >= ones; --j) { + dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1); + } + } + } + + return dp[m][n]; + } +};