diff --git a/cpp/2205/220523.cpp b/cpp/2205/220523.cpp new file mode 100644 index 0000000..96532b8 --- /dev/null +++ b/cpp/2205/220523.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include + +/** + * 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. + */ + +class Solution { +public: + static int findMaxForm(const std::vector& strs, int m, int n) { + std::vector s; + s.reserve(strs.size()); + for (const std::string& str : strs) { + s.push_back((std::count(str.begin(), str.end(), '0') << 8) | std::count(str.begin(), str.end(), '1')); + } + + int ans[101][101]{}; + + for (int c : s) { + int cm = c >> 8, cn = c & 0xFF; + for (int im = m; im >= cm; --im) { + for (int in = n; in >= cn; --in) { + ans[im][in] = std::max(ans[im][in], 1 + ans[im - cm][in - cn]); + } + } + } + + return ans[m][n]; + } +}; + +int main() { + std::cout << Solution::findMaxForm({"10","0001","111001","1","0"}, 5, 3); + return 0; +} diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index 3768d0e..4f8c1e4 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2205 220523-CN.cpp) +ADD_EXECUTABLE(2205 220523.cpp)