From 44cb5af98e0b05976d5e0f361ffcffbd436c607e Mon Sep 17 00:00:00 2001 From: anuradha8055 Date: Wed, 12 Nov 2025 20:55:21 +0800 Subject: [PATCH] leetcode 474 solution --- 474.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 474.cpp 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]; + } +};