id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
0
{ "code": "#include <vector>\n#include <cstring>\n#include <algorithm>\n\nclass Solution {\npublic:\n int dp[101][101];\n\n int findMaxForm(std::vector<std::string>& arr, int m, int n) {\n // Initialize the DP table\n memset(dp, 0, sizeof(dp));\n\n // Traverse each string in the array\n for (const std::string& s : arr) {\n int zero = 0, one = 0;\n \n // Count the number of zeros and ones in the current string\n for (char c : s) {\n if (c == '0') zero++;\n else one++;\n }\n\n // Update the DP table in reverse to avoid overwriting issues\n for (int i = m; i >= zero; --i) {\n for (int j = n; j >= one; --j) {\n dp[i][j] = std::max(dp[i][j], dp[i - zero][j - one] + 1);\n }\n }\n }\n\n return dp[m][n];\n }\n};\n", "memory": "12373" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n int sz = strs.size();\n if(sz == 0)return(0);\n vector<int> arr0(sz, 0);\n vector<int> arr1(sz, 0);\n for(int i=0;i<sz;i++){\n arr0[i] = accumulate(strs[i].begin(), strs[i].end(), 0, [](int total ,char a){\n return 49-a+total;\n });\n arr1[i] = accumulate(strs[i].begin(), strs[i].end(), 0, [](int total , char a){\n return a - 48 + total;\n });\n }\n\n vector<vector<int>> cur(m+1, vector<int>(n+1, 0));\n vector<vector<int>> prev(m+1, vector<int>(n+1, 0));\n\n for(int j=0;j<=m;j++){\n for(int k=0;k<=n;k++){\n if(j >=arr0[0] && k>= arr1[0])cur[j][k] = 1; \n }\n }\n prev = cur;\n\n for(int i=1;i<sz;i++){\n for(int j=0;j<=m;j++){\n for(int k=0;k<=n;k++){\n if(j >=arr0[i] && k>=arr1[i]){\n cur[j][k] = max(cur[j][k], prev[j-arr0[i]][k-arr1[i]] + 1);\n }\n }\n }\n prev = cur;\n }\n\n return(cur[m][n]);\n }\n};", "memory": "14719" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& str, int n, int m) {\n int nn = str.size();\n vector<int>a(nn); vector<int>b(nn); \n vector<vector<int>>dp(n+1,vector<int>(m+1));\n for(int i=0;i<nn;i++){\n string s = str[i];\n for(auto c:s){\n if(c=='0') a[i]++;\n else b[i]++;\n }\n }\n for(int i=0;i<nn;i++){\n int x = a[i]; int y = b[i];\n if(x>n||y>m) continue;\n for(int j=n;j>=x;j--){\n for(int k=m;k>=y;k--){\n dp[j][k] = max(dp[j-x][k-y]+1,dp[j][k]);\n }\n }\n }\n int res = 0;\n for(auto v:dp){\n for(auto x:v) {\n res = max(res,x);\n cout<<x<<\" \";\n }\n cout<<endl;\n }\n return res;\n }\n};", "memory": "14719" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n int maxlength=INT_MIN;\n vector<vector<int>> dp(m+1,vector<int> (n+1,0));\n\n \n for(auto str:strs){\n for(int i=m;i>=zeroes(str);i--){\n for(int j=n;j>=ones(str);j--){\n int take=0;\n \n take=1+dp[i-zeroes(str)][j-ones(str)];\n int nottake=dp[i][j];\n dp[i][j]=max(take,nottake);\n }\n }\n }\n return dp[m][n];\n }\n bool valid(string str,int m,int n){\n int i=0;\n int l=str.size();\n while(i<str.size()){\n if(str[i]=='0'&&m==0){\n return false;\n }\n if(str[i]=='1'&&n==0){\n return false;\n }\n if(str[i]=='0'){\n m--;\n }\n if(str[i]=='1'){\n n--;\n }\n i++;\n }\n return true;\n }\n int zeroes(string str){\n int count=0;\n for(int i=0;i<str.size();i++){\n if(str[i]=='0'){\n count++;\n }\n }\n return count;\n }\n int ones(string str){\n int count=0;\n for(int i=0;i<str.size();i++){\n if(str[i]=='1'){\n count++;\n }\n }\n return count;\n }\n\n};", "memory": "17065" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n int maxlength=INT_MIN;\n vector<vector<int>> dp(m+1,vector<int> (n+1,0));\n\n \n for(auto str:strs){\n for(int i=m;i>=zeroes(str);i--){\n for(int j=n;j>=ones(str);j--){\n int take=0;\n \n take=1+dp[i-zeroes(str)][j-ones(str)];\n int nottake=dp[i][j];\n dp[i][j]=max(take,nottake);\n }\n }\n }\n return dp[m][n];\n }\n bool valid(string str,int m,int n){\n int i=0;\n int l=str.size();\n while(i<str.size()){\n if(str[i]=='0'&&m==0){\n return false;\n }\n if(str[i]=='1'&&n==0){\n return false;\n }\n if(str[i]=='0'){\n m--;\n }\n if(str[i]=='1'){\n n--;\n }\n i++;\n }\n return true;\n }\n int zeroes(string str){\n int count=0;\n for(int i=0;i<str.size();i++){\n if(str[i]=='0'){\n count++;\n }\n }\n return count;\n }\n int ones(string str){\n int count=0;\n for(int i=0;i<str.size();i++){\n if(str[i]=='1'){\n count++;\n }\n }\n return count;\n }\n\n};", "memory": "17065" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\nprivate:\n int helper(vector<pair<int, int>>& data, int zeros, int ones, int idx){\n if(idx >= data.size() or (zeros == 0 and ones == 0)){\n return 0;\n }\n if(dp[idx][zeros][ones] != -1) return dp[idx][zeros][ones];\n if(data[idx].first > zeros or data[idx].second > ones ){\n return dp[idx][zeros][ones] = helper(data, zeros, ones, idx+1);\n }\n return dp[idx][zeros][ones] = max(1 + helper(data, zeros - data[idx].first, ones - data[idx].second, idx+1), helper(data, zeros , ones, idx+1));\n }\npublic:\n int8_t dp[601][101][101];\n int findMaxForm(vector<string>& strs, int m, int n) {\n memset(dp, -1, sizeof(dp));\n int zeros = m;\n int ones = n;\n vector<pair<int, int>> data;\n for( auto i : strs){\n int z = 0;\n int o = 0;\n for (auto j : i){\n j == '0' ? z++ : o++;\n }\n data.push_back({z, o});\n }\n return helper(data, zeros, ones, 0);\n }\n};", "memory": "19411" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n int sz = strs.size();\n int f[sz + 1][m + 1][n + 1];\n memset(f, 0, sizeof(f));\n for (int i = 1; i <= sz; ++i) {\n auto [a, b] = count(strs[i - 1]);\n for (int j = 0; j <= m; ++j) {\n for (int k = 0; k <= n; ++k) {\n f[i][j][k] = f[i - 1][j][k];\n if (j >= a && k >= b) {\n f[i][j][k] = max(f[i][j][k], f[i - 1][j - a][k - b] + 1);\n }\n }\n }\n }\n return f[sz][m][n];\n }\n\n pair<int, int> count(string& s) {\n int a = count_if(s.begin(), s.end(), [](char c) { return c == '0'; });\n return {a, s.size() - a};\n }\n};", "memory": "26450" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n // dp[i][m][n] =max(1+dp[i+1][m-m1][n-n1],dp[i+1][m][n]);\n int sz=strs.size();\n vector<pair<int,int>>count(sz);\n for(int i=0;i<sz;i++){\n for(auto ch:strs[i]){\n if(ch=='0')count[i].first++;\n else count[i].second++;\n }\n }\n int dp[sz+1][m+1][n+1];\n for(int i=sz;i>=0;i--){\n for(int j=0;j<=m;j++){\n for(int k=0;k<=n;k++){\n int ans=0;\n if(i==sz){\n if(m<=0&&n<=0)ans=-1e9;\n }else{\n ans=dp[i+1][j][k];\n int m1=count[i].first;\n int n1=count[i].second;\n if(j-m1>=0&&k-n1>=0){\n ans=max(ans,1+dp[i+1][j-m1][k-n1]);\n }\n }\n dp[i][j][k]=ans;\n }\n }\n }\n return dp[0][m][n];\n }\n};", "memory": "28796" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n int dp[601][101][101];\n\n cout << strs.size() << endl;\n int numZeros[601];\n int numOnes[601];\n\n for(int i = 0; i < strs.size(); i++) {\n int currentOnes = 0;\n int currentZeros = 0;\n for(int j = 0; j < strs[i].size(); j++) {\n if(strs[i][j] == '0') currentZeros++;\n else currentOnes++;\n }\n\n numZeros[i] = currentZeros;\n numOnes[i] = currentOnes;\n }\n\n for(int i = 0; i <= m; i++) {\n for(int j = 0; j <= n; j++) {\n if(numZeros[0] <= i && numOnes[0] <= j) dp[0][i][j] = 1;\n else dp[0][i][j] = 0;\n }\n }\n\n for(int i = 1; i < strs.size(); i++) {\n for(int j = 0; j <= m; j++) {\n for(int k = 0; k <= n; k++) {\n if(j-numZeros[i] >= 0 && k-numOnes[i] >= 0) \n dp[i][j][k] = max(dp[i-1][j][k], 1+dp[i-1][j-numZeros[i]][k-numOnes[i]]);\n else \n dp[i][j][k] = dp[i-1][j][k];\n }\n }\n }\n\n return dp[strs.size()-1][m][n];\n }\n};", "memory": "31143" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n int sz=strs.size();\n int dp[sz][m+1][n+1];\n memset(dp,-1,sizeof(dp));\n function<int(int,int,int)> f=[&](int i,int z,int o)->int{\n if(i==sz) return 0;\n if(dp[i][z][o]!=-1) return dp[i][z][o];\n int skip=f(i+1,z,o);\n int pick=0;\n int strSz=strs[i].size();\n int zeroes=count(begin(strs[i]),end(strs[i]),'0');\n if(zeroes<=z && (strSz-zeroes)<=o) pick=1 + f(i+1,z-zeroes,o-strSz+zeroes);\n return dp[i][z][o]=max(pick,skip);\n };\n return f(0,m,n);\n }\n};", "memory": "33489" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "/*\nIntuition: Similar to knapsack with one additional parameter\ndp[i][j][k] denotes the max length for i ones, j zeros, if we consider first k \nelements of the array\ncurrMax denotes dp[i][j][index_of_currElement]\nWe just have two options:\n1. Take the curr element in subset :\nmax(currMax, dp[i - ones_in_currElement][j - zeros_in_currElement][index _of_currElement- 1] + 1)\n2. Don't take\ndp[i][j][index - 1]\n*/\nclass Solution {\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n int len = strs.size(), j = 0;\n vector<pair<int, int>> zeros(len); //stores number of zeros and ones in each array element\n for(string str : strs) {\n int i = 0;\n for(char c : str) {\n if(c == '1')\n i++;\n }\n zeros[j] = {i, str.size() - i};\n j++;\n };\n int dp[n + 1][m + 1][len + 1];\n memset(dp, 0, sizeof(dp));\n for(int i = 0; i <= n; i++) {\n for(int j = 0; j <= m; j++) {\n for(int index = 1; index <= len; index++) {\n if(i - zeros[index - 1].first >= 0 && j - zeros[index - 1].second >= 0) {\n dp[i][j][index] = max(dp[i][j][index], dp[i - zeros[index - 1].first][j - zeros[index - 1].second][index - 1] + 1);\n }\n dp[i][j][index] = max(dp[i][j][index], dp[i][j][index - 1]);\n }\n }\n }\n \n return dp[n][m][len];\n }\n};", "memory": "35835" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n int k = strs.size();\n int dp[k + 1][m + 1][n + 1];\n memset(dp, 0, sizeof(dp));\n \n for(int i = 1; i < k + 1; i++) {\n auto count = convert(strs[i - 1]);\n auto zeros = count[0];\n auto ones = count[1];\n for(int j = 0; j < m + 1; j++) {\n for(int l = 0; l < n + 1; l++) {\n int res = dp[i - 1][j][l];\n if((j >= zeros) && (l >= ones)) {\n res = max(res, dp[i - 1][j - zeros][l - ones] + 1);\n }\n dp[i][j][l] = res;\n }\n }\n }\n \n return dp[k][m][n];\n \n \n }\n \n vector<int> convert(string s) {\n int zero = 0, one = 0;\n for(auto c: s) {\n if(c == '0') {\n zero++;\n } else if (c == '1') {\n one++;\n }\n }\n \n return {zero, one};\n }\n};", "memory": "35835" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int dp[600][101][101];\n pair<int,int>cnt(string &s){\n int cnt0=0,cnt1=0;\n for(int i=0;i<s.size();i++){\n if(s[i]=='0') cnt0++;\n if(s[i]=='1') cnt1++;\n } \n return {cnt0,cnt1};\n }\n\n int solve(int i, int m, int n, vector<string>&strs){\n if(m<0 || n<0) return -1e9;\n if(i<0) return 0;\n if(dp[i][m][n]!=-1) return dp[i][m][n];\n int way1=solve(i-1,m,n,strs);\n int way2=0;\n auto[cnt0,cnt1]=cnt(strs[i]);\n if(m>=cnt0 && n>=cnt1) way2=1+solve(i-1,m-cnt0,n-cnt1,strs);\n\n return dp[i][m][n]=max(way1,way2);\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n int l=strs.size();\n memset(dp,-1,sizeof(dp));\n return solve(l-1,m,n,strs);\n }\n};", "memory": "38181" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int t[101][101][601];\n int solve(vector<string>& strs, int m, int n,int i){\n if(m<0 || n<0) return -1;\n if(i>=strs.size()) return 0;\n if(t[m][n][i]!=-1) return t[m][n][i];\n int take =0,leave=0;\n int ones = 0,zeros = 0;\n for(char ch:strs[i]){\n if(ch=='1') ones++;\n else zeros++;\n }\n take = 1+solve(strs,m-zeros,n-ones,i+1);\n leave = solve(strs,m,n,i+1);\n return t[m][n][i] = max(take,leave);\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n memset(t,-1,sizeof(t));\n return solve(strs,m,n,0);\n }\n};", "memory": "38181" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int findMaxFormHelper(vector<vector<int>>& freq, int m, int n, int i,int dp[101][101][601]){\n if(m < 0 || n < 0) return INT_MIN+1;\n if(i == freq.size()) return 0;\n if(dp[m][n][i] != -1) return dp[m][n][i];\n int op1 = 1+findMaxFormHelper(freq, m-freq[i][0], n-freq[i][1], i+1, dp);\n int op2 = findMaxFormHelper(freq, m, n, i+1, dp);\n\n\n return dp[m][n][i] = max(op1, op2);\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n\n int dp[101][101][601];\n\n memset(dp, -1, sizeof dp);\n\n\n\n\n\n\n\n int s = strs.size();\n\n vector<vector<int>> freq;\n\n\n for(auto a : strs){\n int ones = 0;\n int zeros = 0;\n for(auto c : a){\n if(c == '0')zeros++;\n else ones++;\n }\n\n freq.push_back({zeros,ones});\n\n }\n return findMaxFormHelper(freq, m, n, 0, dp);\n }\n};", "memory": "40528" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n // 3 D dp vector, as we dicused, We will give it maximum size,\n //see Constraints, 1 <= strs.length <= 600 and 1 <= m, n <= 100\n int dp[601][101][101]; \n \n // Count one and Zero function take string as parameter and count the number of ones and zeroes present in the string and return the counts.\n pair<int, int> countOneAndZero(string s)\n {\n int one = 0, zero = 0;\n \n for(int i = 0; i < s.length(); i++) // travel in the string\n {\n if(s[i] == '1') // if == '1', then add to one\n one++;\n else // otherwise add to zero\n zero++;\n }\n \n return {one, zero};\n }\n \n int solve(int i, int one, int zero, int& maxZero, int& maxOne, \n vector<string>& arr)\n {\n if(i >= arr.size()) // if ith index crosses the length then return 0\n return 0;\n \n // if any of the count, crosses the criteria of having maximum one\n // or zero, then return 0\n if(one > maxOne || zero > maxZero)\n return 0;\n \n // if it is already computed, then no need to do computation again,\n\t\t//return from here itself\n if(dp[i][one][zero] != -1)\n {\n return dp[i][one][zero];\n }\n \n /* what we discused:-\n for every ith index i, we have two option, whether to include it\n in our answer or not, if include then add the count of \n ones and zeros from that string */\n \n // pair p contains, the number of ones and zeroes present in the string of ith index of vector arr.\n pair<int, int> p = countOneAndZero(arr[i]);\n \n /* we declare three variables -\n 1) ans1, If adding the count of ones and zeroes at ith index in arr,\n does not crosses our limit, then to include this in our answer.\n 2) ans2, If adding the count of ones and zeroes at ith index in arr,\n does not crosses our limit, then not to include this in our answer.\n 3) ansWithout, If adding the count of ones and zeroes at ith index in arr, crosses our limit, then not to include this in our answer.\n */\n \n int ans1 = 0, ans2 = 0, ansWithout = 0;\n \n // adding count of current index, not to cross our limit then-\n if(one + p.first <= maxOne && zero + p.second <= maxZero)\n {\n // ans1, including it in our answer\n ans1 = 1 + solve(i + 1, one + p.first, zero + p.second, \n maxZero, maxOne, arr);\n \n // not including in our answer.\n ans2 = solve(i + 1, one, zero, maxZero, maxOne, arr);\n }\n else // if crossing limit, obviously not to take\n {\n ansWithout = solve(i + 1, one, zero, maxZero, maxOne, arr);\n }\n \n // and at last return the maximum of them\n return dp[i][one][zero] = max({ans1, ans2, ansWithout});\n \n \n }\n int findMaxForm(vector<string>& arr, int m, int n) {\n // we redfine m and n as maxzero and maxOne respectively, for better clarification.\n int maxZero = m; \n int maxOne = n;\n \n memset(dp, -1, sizeof(dp)); // intially, putting -1 in dp\n \n /* parameters of solve-\n 1) First parameter is 'i', by which we move in our array of strings.\n 2) Second parameter is the variable who keeps record of count of ones. (we name it as one)\n 3) Third parameter is the variable who keeps record of count of zeroes. (we name it as zero)\n 4) Fourth parameter is maxZero, the maximum zeros we are allowed to take.\n 5) Fifth parameter is maxOne, the maximum ones we are allowed to take. \n 6) Last Sixth parameter is the array itself.\n */\n \n // return solve function\n return solve(0, 0, 0, maxZero, maxOne, arr);\n }\n};", "memory": "40528" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n pair<int, int> count(string str){\n int zero = 0;\n int one = 0;\n for(auto i:str){\n if(i=='0') zero++;\n else one++;\n }\n return {zero,one};\n }\n int findMax(vector<string>&str, int l, int m, int n){\n if(l==0) return 0;\n if(m<0 || n<0) return 0;\n int zeroes = count(str[l-1]).first;\n int ones = count(str[l-1]).second;\n if(m>=zeroes && n>=ones) return max(1+findMax(str,l-1,m-zeroes, n-ones), findMax(str,l-1,m,n));\n else return findMax(str,l-1,m,n);\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n //return findMax(strs, strs.size(), m, n);\n int l = strs.size();\n //vector<vector<vector<int,int>>>(m+1, vector<vector<int>>(n+1, vector<int>(l+1,0)));\n int dp[m+1][n+1][l+1];\n for(int i=0;i<=m;i++){\n for(int j=0;j<=n;j++){\n for(int k=0;k<=l;k++){\n if(k==0) dp[i][j][k] = 0;\n else{\n int zeroes = count(strs[k-1]).first;\n int ones = count(strs[k-1]).second;\n if(i>=zeroes && j>=ones) dp[i][j][k] = max(1+dp[i-zeroes][j-ones][k-1], dp[i][j][k-1]);\n else dp[i][j][k] = dp[i][j][k-1];\n }\n }\n }\n }\n return dp[m][n][l];\n\n }\n};", "memory": "42874" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int dp[605][105][105];\n int solve(int ind,vector<string>& strs, int m, int n){\n if(ind>=strs.size()){\n if(m>=0 && n>=0) return 0;\n else return INT_MIN;\n }\n if(m<0 or n<0) return INT_MIN;\n if(dp[ind][m][n]!=-1) return dp[ind][m][n];\n int one=0,zero=0;\n string s=strs[ind];\n for(auto it:s){\n if(it=='1') one++;\n else zero++;\n }\n int take=1+solve(ind+1,strs,m-zero,n-one);\n int nottake=solve(ind+1,strs,m,n);\n return dp[ind][m][n]=max(take,nottake);\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n memset(dp,-1,sizeof(dp));\n return solve(0,strs,m,n);\n }\n};", "memory": "42874" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int dp[700][101][101];\n vector<string>v;\n int solve(int curr,vector<string>&strs,int m,int n){\n if(curr<0||(m<=0&&n<=0)){\n return 0;\n }\n \n if(dp[curr][m][n]!=0){\n return dp[curr][m][n];\n }\n string a=strs[curr];\n int cnt0=0;\n int cnt1=0;\n for(char c:a){\n if(c=='1'){cnt1++;}\n else cnt0++;\n }\n\n if(cnt1<=n&&cnt0<=m){\n return dp[curr][m][n]=max(1+solve(curr-1,strs,m-cnt0,n-cnt1),solve(curr-1,strs,m,n));\n }\n\n dp[curr][m][n]=solve(curr-1,strs,m,n);\n return dp[curr][m][n];\n\n }\n// int rec(int k,int m,int n){\n// if(k<=0||(m<=0&&n<=0))return 0;//Base Condition.\n// if(t[k][m][n]) return t[k][m][n];\n// string s=v[k-1];\n// int cnt0=0,cnt1=0;//count of 0s and 1s in string.\n// for(auto i:s){\n// if(i=='0')cnt0++;\n// else cnt1++;\n// }\n// if(cnt0>m||cnt1>n) return t[k][m][n]=rec(k-1,m,n);//recursion call if one or both the parameters are small.\n// return t[k][m][n]=max(rec(k-1,m,n),1+rec(k-1,m-cnt0,n-cnt1));//recursion call for finding maximum count.\n// }\n int findMaxForm(vector<string>& strs, int m, int n) {\n // for(int i=0;i<700;i++){\n // for(int j=0;j<700;j++){\n // for(int k=0;k<700;k++) \n // dp[i][j][k]=0;\n // }\n // }\n // v=strs;\n // return rec(v.size(),m,n);\n\n return solve(strs.size()-1,strs,m,n);\n }\n};", "memory": "45220" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n map<int, pair<int, int>> mp;\n int dp[610][110][110];\n int len;\n int recur(int i, int m, int n) {\n if (i == len) {\n return 0;\n }\n if (dp[i][m][n] != -1) {\n return dp[i][m][n];\n }\n int onecnt = mp[i].second;\n int zercnt = mp[i].first;\n int ans = 0;\n if (m >= zercnt and n >= onecnt) {\n ans = max(ans, 1 + recur(i + 1, m - zercnt, n - onecnt));\n }\n ans = max(ans, recur(i + 1, m, n));\n return dp[i][m][n] = ans;\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n len = strs.size();\n for (int i = 0; i < strs.size(); i++) {\n for (auto& itr : strs[i]) {\n if (itr == '1') {\n mp[i].second++;\n } else {\n mp[i].first++;\n }\n }\n }\n memset(dp, -1, sizeof(dp));\n return recur(0, m, n);\n }\n};", "memory": "45220" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int zero, one;\n\n void count(string s){\n zero = 0, one = 0;\n for(int i = 0; i < s.length(); i++){\n if(s[i] == '0') zero++;\n else one++;\n }\n }\n\n int findMaxForm(vector<string>& strs, int m, int n) {\n int dp[660][110][110];\n memset(dp, 0, sizeof(dp));\n int len = strs.size();\n for(int i = len-1; i >= 0; i--){\n count(strs[i]);\n for(int z = 0; z <= m; z++){\n for(int o = 0; o <= n; o++){\n dp[i][z][o] = dp[i+1][z][o];\n if(z >= zero && o >= one) dp[i][z][o] = max(dp[i][z][o], 1 + dp[i+1][z-zero][o-one]);\n }\n }\n }\n return dp[0][m][n];\n }\n};", "memory": "47566" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\nint dp[110][110][800];\n int solve(vector<string>& strs, int m, int n,int i)\n {\n if(m<=0 && n<=0)\n return 0;\n if(i==strs.size())\n return 0;\n if(dp[m][n][i]!=-1)\n return dp[m][n][i];\n int a=0;\n int b=0;\n string s=strs[i];\n for(int j=0;j<s.size();j++)\n {\n if(s[j]=='1')\n a++;\n else\n b++;\n\n }\n int ans=INT_MAX;\n ans= solve(strs,m,n,i+1);\n if(m-b>=0 && n-a>=0)\nans=max( 1+solve(strs,m-b,n-a,i+1),ans);\n return dp[m][n][i]=ans;\n\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n memset(dp,-1,sizeof(dp));\n int ans=solve(strs,m,n,0);\n return ans;\n \n }\n};", "memory": "47566" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "#define ll long long int\nclass Solution {\npublic:\n ll dp[605][105][105];\n ll solve(ll indx, vector<string>& s, ll m, ll n, ll N, vector<ll>&one, vector<ll>&zero){\n if(indx==N){\n if(m>=0 && n>=0) return 0;\n return INT_MIN;\n }\n if(m<0 || n<0) return INT_MIN;\n if(dp[indx][m][n]!=-1) return dp[indx][m][n];\n ll nottake=solve(indx+1,s,m,n,N,one,zero);\n ll take=INT_MIN;\n if(m>=zero[indx] && n>=one[indx]){\n take=1+solve(indx+1,s,m-zero[indx],n-one[indx],N,one,zero);\n }\n return dp[indx][m][n]=max(take,nottake);\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n memset(dp,-1,sizeof(dp));\n ll N=strs.size(), mm=1ll*m, nn=1ll*n;\n vector<ll>one(N,0),zero(N,0);\n for(ll i=0;i<N;i++){\n ll cnt=0;\n for(ll j=0;j<strs[i].size();j++){\n cnt+=strs[i][j]=='1';\n }\n one[i]=cnt;\n zero[i]=strs[i].size()-cnt;\n }\n return solve(0,strs,mm,nn,N,one,zero);\n }\n};", "memory": "49913" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "#define ll long long int\nclass Solution {\npublic:\n ll dp[605][105][105];\n ll solve(ll indx, vector<string>& s, ll m, ll n, ll N, vector<ll>&one, vector<ll>&zero){\n if(indx==N){\n if(m>=0 && n>=0) return 1;\n return INT_MIN;\n }\n if(m<0 || n<0) return INT_MIN;\n if(dp[indx][m][n]!=-1) return dp[indx][m][n];\n ll nottake=solve(indx+1,s,m,n,N,one,zero);\n ll take=INT_MIN;\n if(m>=zero[indx] && n>=one[indx]){\n take=1+solve(indx+1,s,m-zero[indx],n-one[indx],N,one,zero);\n }\n return dp[indx][m][n]=max(take,nottake);\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n memset(dp,-1,sizeof(dp));\n ll N=strs.size(), mm=1ll*m, nn=1ll*n;\n vector<ll>one(N,0),zero(N,0);\n for(ll i=0;i<N;i++){\n ll cnt=0;\n for(ll j=0;j<strs[i].size();j++){\n cnt+=strs[i][j]=='1';\n }\n one[i]=cnt;\n zero[i]=strs[i].size()-cnt;\n }\n return solve(0,strs,mm,nn,N,one,zero)-1;\n }\n};", "memory": "52259" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\nprivate:\n struct Counts {\n int zeros = 0;\n int ones = 0;\n };\npublic:\n int findMaxForm(const vector<string>& strs, int m, int n) {\n vector<int> cache(strs.size() * (m + 1) * (n + 1), -1);\n\n const auto get_cached = [&cache, m, n](int i, int km, int kn) -> int& {\n return cache[i * (m+1) * (n+1) + km * (n+1) + kn];\n };\n\n return largest_subset(strs, 0, m, n, get_cached);\n }\n\nprivate:\n int largest_subset(\n const vector<string>& strs,\n int i,\n int m,\n int n,\n function<int&(int i, int m, int n)> get_cached\n ) {\n if (i >= strs.size()) return 0;\n\n auto& cached = get_cached(i, m, n);\n if (cached != -1) return cached;\n\n const string& s = strs[i];\n\n const int max_exclusive = largest_subset(strs, i+1, m, n, get_cached);\n\n const int n_zeros = count(s.begin(), s.end(), '0');\n const int n_ones = count(s.begin(), s.end(), '1');\n\n if (n_zeros <= m && n_ones <= n) {\n const int max_inclusive = 1 + largest_subset(strs, i+1, m - n_zeros, n - n_ones, get_cached);\n\n return cached = max(max_inclusive, max_exclusive);\n }\n\n return cached = max_exclusive;\n }\n};\n", "memory": "54605" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\nprivate:\n struct Counts {\n int zeros = 0;\n int ones = 0;\n };\npublic:\n int findMaxForm(const vector<string>& strs, int m, int n) {\n vector<int> cache(strs.size() * (m + 1) * (n + 1), -1);\n\n const auto get_cached = [&cache, m, n](int i, int km, int kn) -> int& {\n return cache[i * (m+1) * (n+1) + km * (n+1) + kn];\n };\n\n return largest_subset(strs, 0, m, n, get_cached);\n }\n\nprivate:\n int largest_subset(\n const vector<string>& strs,\n int i,\n int m,\n int n,\n function<int&(int, int, int)> get_cached\n ) {\n if (i >= strs.size() || (!m && !n)) return 0;\n\n auto& cached = get_cached(i, m, n);\n if (cached != -1) return cached;\n\n const string& s = strs[i];\n\n const int max_exclusive = largest_subset(strs, i+1, m, n, get_cached);\n\n const int n_zeros = count(s.begin(), s.end(), '0');\n const int n_ones = count(s.begin(), s.end(), '1');\n\n if (n_zeros <= m && n_ones <= n) {\n const int max_inclusive = 1 + largest_subset(strs, i+1, m - n_zeros, n - n_ones, get_cached);\n\n return cached = max(max_inclusive, max_exclusive);\n }\n\n return cached = max_exclusive;\n }\n};\n", "memory": "54605" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\nprivate:\n struct Counts {\n int zeros = 0;\n int ones = 0;\n };\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<Counts> counts;\n counts.reserve(strs.size());\n\n for (const auto& s: strs) {\n counts.push_back({});\n auto& count = counts.back();\n\n for (const char c: s) {\n if (c == '0') ++count.zeros;\n else ++count.ones;\n }\n }\n\n vector<int> cache(counts.size() * (m + 1) * (n + 1), -1);\n\n const auto get_cached = [&cache, m, n](int i, int km, int kn) -> int& {\n return cache[i * (m+1) * (n+1) + km * (n+1) + kn];\n };\n\n return largest_subset(counts, 0, m, n, get_cached);\n }\n\n int largest_subset(\n const vector<Counts>& counts,\n int i,\n int m,\n int n,\n function<int&(int i, int m, int n)> get_cached\n ) {\n if (i >= counts.size()) return 0;\n\n auto& cached = get_cached(i, m, n);\n if (cached != -1) return cached;\n\n const auto& c = counts[i];\n\n const int max_exclusive = largest_subset(counts, i+1, m, n, get_cached);\n\n if (c.zeros <= m && c.ones <= n) {\n const int max_inclusive = 1 + largest_subset(counts, i+1, m - c.zeros, n - c.ones, get_cached);\n\n return cached = max(max_inclusive, max_exclusive);\n }\n\n return cached = max_exclusive;\n }\n};\n", "memory": "56951" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<pair<int, int>> num(strs.size(), std::pair<int, int>(0, 0));\n for(int i = 0; i < strs.size(); ++i) {\n for(char ch: strs[i]) {\n if (ch == '0') {\n ++num[i].first;\n } else {\n ++num[i].second;\n }\n }\n // std::cout << zeros[i] << \" \" << ones[i] << std::endl;\n }\n\n\n std::sort(num.begin(), num.end(), [&m, &n] (const auto& lhs, const auto& rhs) {\n return std::max(static_cast<double>(lhs.first) / m, static_cast<double>(lhs.second) / n) < std::max(static_cast<double>(rhs.first) / m, static_cast<double>(rhs.second) / n); \n });\n\n for(const auto& p: num) {\n // std::cout << p.first << \" \" << p.second << std::endl;\n }\n\n priority_queue<pair<pair<int, int>, int>> pq; // ((max possible value, value), <zeroes, ones, idx>)\n pq.push({{PossibleValue(num[0].first, num[0].second, m, n), 0}, 0});\n\n vector<int> best_value ((num.size() + 1) * (n + 1) * (m + 1), -1);\n best_value[0] = 0;\n int result = 0;\n\n while (!pq.empty()) {\n auto pr = pq.top();\n int possible_value = pr.first.first;\n if (possible_value * 3 < result) {\n return result;\n }\n int value = pr.first.second;\n int zeros = pr.second % (m + 1);\n pr.second /= (m + 1);\n int ones = pr.second % (n + 1);\n int pos = pr.second / (n + 1);\n pq.pop();\n\n // std::cout << zeros << \" \" << ones << \" \" << pos << \" \" << pr.second << \" \" << value << \" \" << possible_value << std::endl;\n\n int state_no_take = State(zeros, ones, pos + 1, m, n);\n if (best_value[state_no_take] < value) {\n best_value[state_no_take] = value;\n if (pos + 1 < num.size()) {\n pq.push({{PossibleValue(num[pos + 1].first, num[pos + 1].second, m, n), value}, state_no_take});\n } else {\n result = max(result, value);\n }\n }\n if (zeros + num[pos].first <= m && ones + num[pos].second <= n) {\n int state_take = State(zeros + num[pos].first, ones + num[pos].second, pos + 1, m, n);\n if (best_value[state_take] < value + 1) {\n best_value[state_take] = value + 1;\n if (pos + 1 < num.size()) {\n pq.push({{PossibleValue(num[pos + 1].first, num[pos + 1].second, m, n), value + 1}, state_take});\n } else {\n result = max(result, value + 1);\n }\n }\n }\n\n }\n\n return result;\n }\n\n int State(int zeros, int ones, int pos, int m, int n) {\n return zeros + ones * (m + 1) + pos * (n + 1) * (m + 1);\n }\n\n int PossibleValue(int zeros, int ones, int m, int n) {\n if (ones == 0) {\n return m / zeros;\n }\n if (zeros == 0) {\n return n / ones;\n }\n return std::min(n / ones, m / zeros);\n }\n};", "memory": "59298" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n\n int back(vector<string> &strs, int m, int n, vector<vector<vector<int>>> &dp, int i) {\n if(i==strs.size()) return 0;\n if(dp[m][n][i]!=-1) return dp[m][n][i];\n int zero = count(strs[i].begin(), strs[i].end(), '0');\n int one = strs[i].size()-zero;\n int inc = -1;\n if(m>=zero && n>=one) {\n inc = back(strs, m-zero, n-one, dp, i+1);\n if(inc!=-1) inc++;\n }\n int exc = back(strs, m, n, dp, i+1);\n return dp[m][n][i] = max(inc, exc);\n }\n\n int findMaxForm(vector<string>& strs, int m, int n) {\n int x = strs.size();\n vector<vector<vector<int>>> dp(m+1, vector<vector<int>>(n+1, vector<int>(x, -1)));\n return back(strs, m, n, dp, 0);\n }\n};", "memory": "61644" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int func(int m, int n, int i,vector<string>&s, vector<vector<vector<int>>> &dp)\n {\n if(i==s.size())return 0;\n if(dp[m][n][i]!=-1)return dp[m][n][i];\n int z=0,o=0;\n for(int j=0;j<s[i].length();j++)s[i][j]=='1'?o++:z++;\n if(z<=m && o<=n)\n {\n return dp[m][n][i]=max(1+func(m-z,n-o,i+1,s,dp),func(m,n,i+1,s,dp));\n }\n else return dp[m][n][i]=func(m,n,i+1,s,dp);\n }\n int findMaxForm(vector<string>& s, int m, int n) {\n vector<vector<vector<int>>> dp(m+1,vector<vector<int>>(n+1,vector<int>(s.size(),-1)));\n return func(m,n,0,s,dp);\n }\n};", "memory": "63990" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n int len=strs.size();\n vector<vector<vector<int>>> dp(m+1, vector<vector<int>>(n+1, vector<int>(len, -1)));\n\n auto f=[&](int m,int n,int ind, auto &&f)->int{\n if(ind==len) return 0;\n if(dp[m][n][ind]!=-1) return dp[m][n][ind];\n int z=0,o=0;\n for(auto ch: strs[ind]){\n if(ch=='0') z++;\n else o++;\n }\n int notTake=f(m,n,ind+1,f);\n int take=INT_MIN;\n if(m-z>=0 && n-o>=0) take=1+f(m-z,n-o,ind+1,f);\n return dp[m][n][ind]=max(take,notTake);\n };\n return f(m,n,0,f);\n }\n};", "memory": "66336" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n // Initialize memoization table with dimensions [m + 1][n + 1][strs.size()], filled with -1\n vector<vector<vector<int>>> memo(m + 1, vector<vector<int>>(n + 1, vector<int>(strs.size(), -1)));\n return dp(0, 0, strs, m, n, 0, memo);\n }\n\n int dp(int totalZeros, int totalOnes, vector<string>& strs, int m, int n, int i, vector<vector<vector<int>>> &memo){\n // Base case: If we've considered all strings\n if (i == strs.size()) return 0;\n\n // Check if result is already computed\n if (memo[totalZeros][totalOnes][i] != -1) {\n return memo[totalZeros][totalOnes][i];\n }\n\n int zeros = 0;\n int ones = 0;\n // Count zeros and ones in the current string\n for (char c : strs[i]) {\n if (c == '0') zeros++;\n else ones++;\n }\n\n int use = 0, skip = 0;\n\n // Decide whether to include the current string\n if (totalZeros + zeros <= m && totalOnes + ones <= n) {\n use = 1 + dp(totalZeros + zeros, totalOnes + ones, strs, m, n, i + 1, memo);\n }\n\n // Skip the current string\n skip = dp(totalZeros, totalOnes, strs, m, n, i + 1, memo);\n\n // Memoize and return the maximum of including or skipping the current string\n memo[totalZeros][totalOnes][i] = max(use, skip);\n return memo[totalZeros][totalOnes][i];\n }\n};\n", "memory": "68683" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int solve(vector<string>&nums,int n,int m,int index,vector<vector<vector<int>>>&dp){\n if(index == nums.size()){\n return 0;\n }\n\n if(dp[n][m][index] != -1){\n return dp[n][m][index];\n }\n\n int zero = 0;\n int one = 0;\n for(int i=0;i<nums[index].size();i++){\n if(nums[index][i] == '0'){\n zero++;\n }\n else{\n one++;\n }\n }\n\n int ans1 = 0;\n int ans2 = 0;\n\n if(zero <= m && one <= n){\n ans1 = 1 + solve(nums,n-one,m-zero,index+1,dp);\n }\n\n ans2 = solve(nums,n,m,index+1,dp);\n\n dp[n][m][index] = max(ans1,ans2);\n\n return dp[n][m][index];\n }\n \n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<vector<vector<int>>>dp(n+1,vector<vector<int>>(m+1,vector<int>(strs.size(),-1)));\n return solve(strs,n,m,0,dp);\n }\n};", "memory": "71029" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<vector<vector<int>>> v(m + 1, vector<vector<int>>(n + 1, vector<int>(strs.size(), -1)));\n vector<int> z;\n vector<int> o;\n for (auto& s : strs){\n int oneC = 0, zeroC = 0;\n for (auto& c : s)\n if (c == '1') ++oneC;\n else ++zeroC;\n z.push_back(zeroC);\n o.push_back(oneC);\n }\n return dfs(v, z, o, m, n, 0);\n }\n int dfs(vector<vector<vector<int>>>& v, vector<int>& z, vector<int>& o, int m, int n, int ind){\n if (ind == z.size()) return 0;\n if (v[m][n][ind] != -1) return v[m][n][ind];\n return v[m][n][ind] = z[ind] <= m && o[ind] <= n ?\n max(1 + dfs(v, z, o, m - z[ind], n - o[ind], ind + 1), dfs(v, z, o, m, n, ind + 1)) :\n dfs(v, z, o, m, n, ind + 1);\n }\n};", "memory": "73375" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int solve(vector<pair<int,int>>& count, int m, int n, int index, vector<vector<vector<int>>>& dp){\n if(index >= count.size()){\n return 0;\n }\n\n if(dp[m][n][index] != -1){\n return dp[m][n][index];\n }\n\n int zeros = count[index].first;\n int ones = count[index].second;\n int include = 0;\n // This line is important. At most m-0's and n-1's\n if(m-zeros >=0 && n-ones>=0){\n include = 1 + solve(count, m-zeros, n-ones, index+1, dp);\n \n }\n\n int exclude = solve(count, m, n, index+1, dp);\n\n return dp[m][n][index] = max(include, exclude);\n }\n\n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<pair<int, int>> count;\n for(int i=0;i<strs.size();i++){\n int j=0;\n int zeros=0, ones=0;\n while(j<strs[i].length()){\n if(strs[i][j]=='0'){\n zeros++;\n }\n else{\n ones++;\n }\n j++;\n }\n count.push_back({zeros,ones});\n }\n\n vector<vector<vector<int>>> dp (m+1, vector<vector<int>>(n+1, vector<int> (strs.size(), -1)));\n int ans = solve(count, m, n, 0, dp);\n\n return ans;\n }\n};", "memory": "75721" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\nprivate:\n vector<vector<vector<int>>> dp;\n int dfs(vector<vector<int>>& count, int m, int n, int from) {\n if (from >= count.size())\n return 0;\n int& r = dp[m][n][from];\n if (r >= 0)\n return r;\n \n int temp = dfs(count, m, n, from + 1);\n if (count[from][0] <= m && count[from][1] <= n)\n temp = max(temp, dfs(count, m - count[from][0], n - count[from][1], from + 1) + 1);\n r = temp;\n return r;\n }\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<vector<int>> count(strs.size(), vector<int>(2, 0));\n for (int i = 0; i < strs.size(); i++) {\n for (char c : strs[i])\n count[i][c - '0']++;\n }\n dp.assign(m + 1, vector<vector<int>>(n + 1, vector<int>(strs.size(), -1)));\n return dfs(count, m, n, 0);\n }\n};", "memory": "78068" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n\n int util (vector<pair<short, short>> &nums, int m, int n, int idx, vector <vector<vector<int>>> &dp) {\n if (idx == nums.size()) return 0;\n if (m <= 0 && n <= 0) return 0;\n\n if (dp [m][n][idx] != -1) return dp [m][n][idx];\n\n int s1 = 0;\n int a = nums[idx].first;\n int b = nums[idx].second;\n if (a <= m && b <= n) {\n s1 = 1 + util (nums, m - a, n - b, idx + 1, dp);\n }\n int s2 = util (nums, m, n, idx+1, dp);\n\n dp [m][n][idx] = max (s1, s2);\n return dp [m][n][idx];\n }\n\n\n int findMaxForm(vector<string>& strs, int m, int n) {\n\n vector <pair<short, short>> nums;\n\n int s = strs.size();\n vector <vector<vector<int>>> dp (m+1, vector <vector<int>> (n+1, vector<int> (s, -1)));\n\n for (auto str : strs) {\n int ones = 0;\n for (auto ch : str) {\n if (ch == '1') ones++;\n }\n nums.push_back (pair<int, int> (str.size() - ones, ones));\n }\n\n return util(nums, m, n, 0, dp);\n }\n};", "memory": "80414" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\nprivate:\n int solve(vector<pair<int,int>>&nums, int num0, int num1, int index, vector<vector<vector<int>>> &dp) {\n if(index == nums.size() || (num0 == 0 && num1 == 0)) {\n return 0;\n }\n\n if(dp[num0][num1][index] != -1) {\n return dp[num0][num1][index];\n }\n\n if(nums[index].first > num0 || nums[index].second > num1) {\n return solve(nums,num0,num1,index+1,dp);\n } \n\n int include = 1 + solve(nums, num0-nums[index].first, num1-nums[index].second, index+1,dp);\n int exclude = solve(nums,num0,num1,index+1,dp);\n\n return dp[num0][num1][index] = max(include,exclude);\n }\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<pair<int,int>> nums;\n vector<vector<vector<int>>> dp(m+1,vector<vector<int>>(n+1,vector<int>(strs.size(),-1)));\n for(auto i : strs) {\n int num1 = 0, num0 = 0;\n\n for(auto ch : i) {\n (ch == '0') ? num0++ : num1++;\n }\n\n nums.push_back({num0,num1});\n }\n\n return solve(nums,m,n,0,dp);\n }\n};", "memory": "82760" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n // what choices do you have and what is your capacity defines the problem.\n // dp[n][n][i] = max(dp[m][m][i+1] + dp[m-zeroes][n-ones][i+1])\n // count ones and zeroes in a string in cpp\n // vector<vector<vector<int> > > v(2, vector<vector<int> >(3, vector<int>(4)));\n int s = strs.size();\n vector<vector<vector<int>>> dp(m + 1, vector<vector<int>>(n + 1, vector<int>(s + 1)));\n for(int i = strs.size()-1;i>=0;i--){\n // for every index and every combination of capacity, the problem is being solved for.\n for(int j = 0;j<=m;j++){\n for(int k = 0;k<=n;k++){\n int zeros = count(begin(strs[i]),end(strs[i]),'0');\n int ones = count(begin(strs[i]),end(strs[i]),'1');\n // choose or not choose.\n if(j>=zeros and k>= ones) dp[j][k][i] = max(1 + dp[j-zeros][k-ones][i+1],dp[j][k][i+1]);\n else dp[j][k][i] = dp[j][k][i+1];\n }\n } \n }\n return dp[m][n][0];\n }\n};", "memory": "85106" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int helper(vector<string>& strs, int m, int n,int ind,vector<vector<vector<int>>>&dp){\n if(ind==strs.size()){\n return 0;\n }\n if(dp[m][n][ind]!=-1){\n return dp[m][n][ind];\n }\n int cntZeros=count(strs[ind].begin(),strs[ind].end(),'0');\n int cntOnes=strs[ind].size()-cntZeros;\n if(m-cntZeros>=0 && n-cntOnes>=0){\n return dp[m][n][ind]=max(1+helper(strs,m-cntZeros,n-cntOnes,ind+1,dp),helper(strs,m,n,ind+1,dp));\n }\n return dp[m][n][ind]=helper(strs,m,n,ind+1,dp);\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<vector<vector<int>>>dp(m+1,vector<vector<int>>(n+1,vector<int>(strs.size()+1,-1)));\n return helper(strs,m,n,0,dp);\n }\n};", "memory": "87453" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n int len = strs.size();\n vector<pair<int,int>> vii(len, {0,0}); // { zeros_count, ones_count }\n vector<vector<vector<int>>> memo(m+1, vector<vector<int>> (n+1,vector<int> (len+1,-1) ));\n\n for(int i=0;i<len;i++)\n for(char &c: strs[i])\n c=='0' ? vii[i].first++ : vii[i].second++;\n \n return f(vii, m, n, 0, memo);\n }\n\n int f(vector<pair<int,int>> &vii,int m, int n, int i, vector<vector<vector<int>>> &memo ){\n int len = vii.size();\n if( ( m==0 && n==0 ) || i==len) return 0;\n\n if(memo[m][n][i] != -1) return memo[m][n][i];\n\n int skip = f(vii, m , n, i+1, memo);\n int include;\n \n if(vii[i].first<=m && vii[i].second<=n)\n include = f(vii, m - vii[i].first, n - vii[i].second, i+1, memo) + 1;\n \n return memo[m][n][i] = max(skip, include);\n }\n};", "memory": "89799" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int count(int i,char val,vector<string>& strs){\n int count = 0;\n\n for(int j = 0;j<strs[i].size();j++){\n if (strs[i][j]==val){\n count++;\n }\n }\n return count;\n }\n \n int rec(int i,int one,int zero,vector<string>& strs, int m, int n,vector<vector<vector<int>>> &dp){\n if (i>=strs.size()) return 0;\n\n if (dp[i][one][zero]!=-1) return dp[i][one][zero];\n\n int pick = 0;\n\n int x = count(i,'1',strs);\n int y = count(i,'0',strs);\n\n if (x+one<=n && y+zero<=m){\n pick = 1+rec(i+1,one+x,zero+y,strs,m,n,dp);\n }\n int not_pick = rec(i+1,one,zero,strs,m,n,dp);\n\n dp[i][one][zero]= max(pick,not_pick);\n return dp[i][one][zero];\n }\n\n int findMaxForm(vector<string>& strs, int m, int n) {\n int k = strs.size();\n vector<vector<vector<int>>> dp(k, vector<vector<int>>(n+1, vector<int>(m+1, -1)));\n\n return rec(0,0,0,strs,m,n,dp);\n }\n};", "memory": "99184" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int solve(vector<pair<int,int>>& v,int m,int n,int ind,int c1,int c0,vector<vector<vector<int>>>& dp){\n if(ind>=v.size()){\n if(c1<=n && c0<=m){return 0;}\n return -1e9;\n }\n if(c1>n || c0>m){return -1e9;}\n if(dp[ind][c1][c0]!=-1){\n return dp[ind][c1][c0];\n }\n int pick=1+solve(v,m,n,ind+1,c1+v[ind].first,c0+v[ind].second,dp);\n int notpick=solve(v,m,n,ind+1,c1,c0,dp);\n\n return dp[ind][c1][c0]=max(pick,notpick);\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<pair<int,int>>v;\n for(int i=0;i<strs.size();i++){\n int one=0;\n for(int j=0;j<strs[i].length();j++){\n one+=(strs[i][j]-'0');\n }\n v.push_back({one,strs[i].length()-one});\n }\n vector<vector<vector<int>>>dp(v.size(),vector<vector<int>>(n+1,vector<int>(m+1,-1)));\n\n return solve(v,m,n,0,0,0,dp);\n }\n};", "memory": "101530" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<vector<vector<int>>> dp;\n int help(vector<pair<int, int>>& a, int i, int one, int zero, int m,\n int n) {\n if (one > n || zero > m)\n return INT_MIN;\n if (i == a.size())\n return 0;\n\n if (dp[i][one][zero] != -1)\n return dp[i][one][zero];\n\n int skip = help(a, i + 1, one, zero, m, n);\n int take = 0;\n\n take = 1 + help(a, i + 1, one + a[i].second, zero + a[i].first, m, n);\n\n return dp[i][one][zero] = max(take, skip);\n }\n int findMaxForm(vector<string>& v, int m, int n) {\n\n dp.resize(v.size(), vector<vector<int>>(n + 1, vector<int>(m + 1, -1)));\n\n // {zero,one}\n vector<pair<int, int>> a;\n for (auto i : v) {\n int x = i.size();\n int one = 0;\n for (auto c : i) {\n if (c == '1')\n ++one;\n }\n a.push_back({x - one, one});\n }\n\n return help(a, 0, 0, 0, m, n);\n }\n};", "memory": "103876" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int cnt1(string &str)\n {\n int cnt=0;\n for(auto i:str)\n {\n if(i=='1')\n cnt++;\n }\n return cnt;\n }\nint solve(vector<string>&strs,int m,int n,int index, vector<vector<vector<int>>>&dp)\n {\n if(index==strs.size())\n return 0;\n if(dp[index][m][n]!=-1)\n return dp[index][m][n];\n if(cnt1(strs[index])<=m && strs[index].size()-cnt1(strs[index])<=n)\n {\nreturn dp[index][m][n]=max(1+solve(strs,m-cnt1(strs[index]),n+cnt1(strs[index])\n-strs[index].size(),index+1,dp)\n,solve(strs,m,n,index+1,dp));\n\n }\n else\n return dp[index][m][n]=solve(strs,m,n,index+1,dp); \n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<vector<vector<int>>>dp(strs.size()+1,vector<vector<int>>\n (n+1,vector<int>(m+1,-1)));\n return solve(strs,n,m,0,dp);\n }\n};", "memory": "103876" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n pair<int , int> count(string str){\n int cnt0 = 0 , cnt1 = 0;\n for(int i = 0 ; i < str.size() ; i++){\n if(str[i] == '0'){\n cnt0++;\n }else if(str[i] == '1'){\n cnt1++;\n }\n }\n return {cnt0 , cnt1};\n }\n\n // int solve(vector<string>& strs, int ind , int m, int n ,vector<vector<vector<int>>>& dp){\n // if (m == 0 && n == 0){\n // return 0;\n // }\n // if(ind == 0){\n // auto counts = count(strs[0]);\n // if(counts.first <= m && counts.second <= n) return 1;\n // else return 0;\n // }\n // if(dp[ind][m][n] != -1) return dp[ind][m][n];\n // int notake = solve(strs , ind-1 , m , n ,dp);\n // int take = 0;\n // pair<int,int>counts = count(strs[ind]);\n // if(counts.first <= m && counts.second <= n){\n // take = 1 + solve(strs , ind-1 , m - counts.first , n - counts.second , dp);\n // }\n\n // return dp[ind][m][n] = max(take , notake);\n\n // }\n int findMaxForm(vector<string>& strs, int m, int n) {\n int ind = strs.size();\n vector<vector<vector<int>>>dp (ind , vector<vector<int>>(m+1 , vector<int>(n+1 , 0)));\n auto counts = count(strs[0]);\n for(int i = 0 ; i <= m ; i++){\n for(int j = 0 ; j <= n ; j++){\n if (counts.first <= i && counts.second <= j) {\n dp[0][i][j] = 1; \n }\n }\n }\n for(int i = 1 ; i < ind ; i++){\n auto counts = count(strs[i]);\n for(int j = 0 ; j <= m ; j++){\n for(int k = 0 ; k <= n ; k++ ){\n int notake = dp[i-1][j][k];\n int take = 0;\n if(counts.first <= j && counts.second <= k){\n take = 1 + dp[i-1][j-counts.first][k - counts.second];\n }\n dp[i][j][k] = max(take, notake);\n }\n }\n } \n\n return dp[ind-1][m][n];\n }\n};", "memory": "106223" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int f(vector<vector<vector<int>>> &dp, vector<string>& strs, vector<int> &countOne, int m, int n, int zeroCount, int oneCount, int index){\n if(index < 0) return 0;\n if(zeroCount>m || oneCount>n) return 0;\n if(dp[index][zeroCount][oneCount]!=-1) return dp[index][zeroCount][oneCount];\n \n int take = 0;\n int zeroCurr = strs[index].size() - countOne[index];\n if(oneCount + countOne[index]<= n && zeroCount + zeroCurr <=m) \n take = 1 + f(dp, strs, countOne, m, n, zeroCount + zeroCurr, oneCount + countOne[index], index-1);\n\n int notTake = f(dp, strs, countOne, m, n, zeroCount, oneCount, index-1);\n\n return dp[index][zeroCount][oneCount] = max(take, notTake);\n }\n\n int countOneInString(string s){\n int count = 0;\n for(int i = 0; i<s.size(); i++) if(s[i]=='1') count++;\n return count;\n }\n\n int findMaxForm(vector<string>& strs, int m, int n) {\n int sizeStrs = strs.size();\n vector<vector<vector<int>>> dp(sizeStrs, vector<vector<int>>(m+1, vector<int>(n+1, -1)));\n\n vector<int> countOne(sizeStrs, 0);\n\n for(int i = 0; i<sizeStrs; i++) countOne[i] = countOneInString(strs[i]);\n\n return f(dp, strs, countOne, m, n, 0, 0, sizeStrs-1);\n }\n};", "memory": "106223" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n pair<int , int> count(string str){\n int cnt0 = 0 , cnt1 = 0;\n for(int i = 0 ; i < str.size() ; i++){\n if(str[i] == '0'){\n cnt0++;\n }else if(str[i] == '1'){\n cnt1++;\n }\n }\n return {cnt0 , cnt1};\n }\n\n int solve(vector<string>& strs, int ind , int m, int n ,vector<vector<vector<int>>>& dp){\n if (m == 0 && n == 0){\n return 0;\n }\n if(ind == 0){\n auto counts = count(strs[0]);\n if(counts.first <= m && counts.second <= n) return 1;\n else return 0;\n }\n if(dp[ind][m][n] != -1) return dp[ind][m][n];\n int notake = solve(strs , ind-1 , m , n ,dp);\n int take = 0;\n pair<int,int>counts = count(strs[ind]);\n if(counts.first <= m && counts.second <= n){\n pair<int,int>counts = count(strs[ind]);\n take = 1 + solve(strs , ind-1 , m - counts.first , n - counts.second , dp);\n }\n\n return dp[ind][m][n] = max(take , notake);\n\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n int ind = strs.size();\n vector<vector<vector<int>>>dp (ind , vector<vector<int>>(m+1 , vector<int>(n+1 , -1)));\n\n return solve(strs , ind-1 , m , n , dp);\n }\n};", "memory": "108569" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int si = 0;\n int cnt0(string s) {\n int cnt = 0;\n for (auto it : s) {\n if (it == '0')\n cnt++;\n }\n return cnt;\n }\n int helper(vector<string>& strs, int i, int m, int n,vector<vector<vector<int>>>&dp) {\n if (i == 0) {\n string s = strs[i];\n int zeroes = cnt0(s);\n int ones = s.size() - zeroes;\n if (zeroes <= m && ones <= n) {\n return 1;\n } else {\n return 0;\n }\n }\n if (m == 0 && n == 0)\n return 0;\n if(dp[i][m][n]!=-1)return dp[i][m][n];\n int include = -1;\n int notinclude = helper(strs, i - 1, m, n,dp);\n string s = strs[i];\n int zeroes = cnt0(s);\n int ones = s.size() - zeroes;\n if (zeroes <= m && ones <= n) {\n include = 1 + helper(strs, i - 1, m - zeroes, n - ones,dp);\n }\n return dp[i][m][n] = max(include, notinclude);\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n si = strs.size();\n vector<vector<vector<int>>>dp(si,vector<vector<int>>(m+1,vector<int>(n+1,-1)));\n // vector<int> mvalue(si + 1, 0);\n // vector<int> nvalue(si + 1, 0);\n // vector<int> dp(si + 1, 0);\n // mvalue[0] = m;\n // nvalue[0] = n;\n // int ans = 0;\n\n // for (int i = 1; i <= si; i++) {\n\n // string s = strs[i - 1];\n\n // int zeroes = cnt0(s);\n // int ones = s.size() - zeroes;\n // for (int j = 0; j < i; j++) {\n // if (zeroes <= mvalue[j] && ones <= nvalue[j]) {\n // if (dp[i] <= dp[j]) {\n // dp[i] = dp[j] + 1;\n // mvalue[i] = mvalue[j] - zeroes;\n // nvalue[i] = nvalue[j] - ones;\n // } else {\n // }\n // }\n // }\n // ans = max(ans, dp[i]);\n // }\n return helper(strs,si-1,m,n,dp);\n }\n};", "memory": "108569" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n int i = 0;\n vector<vector<vector<int>>> dp(\n strs.size() + 1, vector<vector<int>>(m + 1, vector<int>(n + 1, 0)));\n\n vector<vector<int>>curr(m + 1, vector<int>(n + 1, 0));\n vector<vector<int>>after(m + 1, vector<int>(n + 1, 0));\n\n for (int i = strs.size() - 1; i >= 0; i--) {\n for (int j = 0; j <= m; j++) {\n for (int k = 0; k <= n; k++) {\n // take\n string s = strs[i];\n int one = 0, zero = 0;\n for (auto it : s) {\n if (it == '0')\n zero++;\n else\n one++;\n }\n int take = 0;\n if (j - zero >= 0 && k - one >= 0)\n take = 1 + after[j - zero][k - one];\n\n // not-take\n int nottake = after[j][k];\n\n curr[j][k] = max(take, nottake);\n }\n }\n after = curr;\n }\n\n return curr[m][n];\n }\n};", "memory": "110915" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int count_zeroes(string s){\n int count =0;\n for(int i=0;i<s.size();i++){\n if(s[i]=='0'){\n count=count+1;\n }\n }\n return count;\n }\n int count_ones(string s){\n int count =0;\n for(int i=0;i<s.size();i++){\n if(s[i]=='1'){\n count=count+1;\n }\n }\n return count;\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<vector<vector<int>>>dp(strs.size()+1,vector<vector<int>>(m+1,vector<int>(n+1,0)));\n for(int i = strs.size()-1;i>=0;i--)\n {\n for( int j=0;j<=m;j++)\n {\n for(int k=0;k<=n;k++)\n {\n int zeroes = count_zeroes(strs[i]);\n int ones = count_ones(strs[i]);\n int include=0,exclude=0;\n\n if(j-zeroes >=0 && k-ones>=0)\n {\n include = 1 + dp[i+1][j-zeroes][k-ones];\n }\n exclude = dp[i+1][j][k];\n \n dp[i][j][k] = max(include,exclude);\n }\n }\n }\n\n return dp[0][m][n];\n \n }\n};", "memory": "110915" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\nint dp[602][200][200];\nint solve(int index , unordered_map<int,pair<int,int>> &map ,int ones , int zeros, int m , int n , int size){\n if(zeros>m || ones>n){\n return INT_MIN;\n }\n if(index==size){\n return 0;\n }\n if(dp[index][ones][zeros] !=-1){\n return dp[index][ones][zeros];\n }\n int inc = 1+solve(index+1,map,ones+map[index].first,zeros+map[index].second , m , n , size);\n int exc = solve(index+1,map,ones,zeros,m,n,size);\n return dp[index][ones][zeros]= max(inc,exc);\n}\npublic:\n\n int findMaxForm(vector<string>& strs, int m, int n) {\n memset(dp,-1,sizeof(dp));\n unordered_map<int,pair<int,int>> map;\n for(int i=0 ; i<strs.size() ; i++){\n string s = strs[i];\n int ones=0;\n int zeros=0;\n for(int j=0 ; j<s.length() ; j++){\n if(s[j]=='1'){\n ones++;\n }\n else{\n zeros++;\n }\n }\n cout<<ones<<\" \"<<zeros<<endl;\n map[i]={ones,zeros};\n }\n return solve(0,map,0,0,m,n,strs.size());\n }\n};", "memory": "113261" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\nint dp[602][200][200];\nint solve(int index , unordered_map<int,pair<int,int>> &map ,int ones , int zeros, int m , int n , int size){\n if(zeros>m || ones>n){\n return INT_MIN;\n }\n if(index==-1 ){\n return 0;\n }\n if(dp[index][ones][zeros] !=-1){\n return dp[index][ones][zeros];\n }\n int inc = 1+solve(index-1,map,ones+map[index].first,zeros+map[index].second , m , n , size);\n int exc = solve(index-1,map,ones,zeros,m,n,size);\n return dp[index][ones][zeros]= max(inc,exc);\n}\npublic:\n\n int findMaxForm(vector<string>& strs, int m, int n) {\n memset(dp,-1,sizeof(dp));\n unordered_map<int,pair<int,int>> map;\n for(int i=0 ; i<strs.size() ; i++){\n string s = strs[i];\n int ones=0;\n int zeros=0;\n for(int j=0 ; j<s.length() ; j++){\n if(s[j]=='1'){\n ones++;\n }\n else{\n zeros++;\n }\n }\n cout<<ones<<\" \"<<zeros<<endl;\n map[i]={ones,zeros};\n }\n return solve(strs.size()-1,map,0,0,m,n,strs.size());\n }\n};", "memory": "115608" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nvector<string> strs;\n\nint memo[601][202][202];\n int cnt(string& s) {\n int res = 0;\n for(int i = 0; i < s.size(); i++) {\n res += s[i] == '1';\n }\n return res;\n }\n int sol(int i, int m, int n) {\n if(i == strs.size() or (m == 0 and n == 0)) return 0;\n\n\n if(memo[i][m + 100][n + 100] != -1) \n return memo[i][m + 100][n + 100];\n\n int cnt1s = cnt(strs[i]);\n int cnt0s = strs[i].size() - cnt1s;\n if(cnt1s > n or cnt0s > m) \n return sol(i + 1, m, n);\n int take, skip;\n take = 1 + sol(i + 1, m - cnt0s, n - cnt1s);\n skip = sol(i + 1, m, n);\n\n return memo[i][m + 100][n + 100] = max(take, skip);\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n this->strs = strs;\n memset(memo, -1, sizeof(memo));\n return sol(0, m, n);\n }\n};", "memory": "117954" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nvector<string> strs;\n\nint memo[601][202][202];\n int cnt(string& s) {\n int res = 0;\n for(int i = 0; i < s.size(); i++) {\n res += s[i] == '1';\n }\n return res;\n }\n int sol(int i, int m, int n) {\n if(i == strs.size() or (m == 0 and n == 0)) return 0;\n\n\n if(memo[i][m][n] != -1) \n return memo[i][m][n];\n\n int cnt1s = cnt(strs[i]);\n int cnt0s = strs[i].size() - cnt1s;\n if(cnt1s > n or cnt0s > m) \n return sol(i + 1, m, n);\n int take, skip;\n take = 1 + sol(i + 1, m - cnt0s, n - cnt1s);\n skip = sol(i + 1, m, n);\n\n return memo[i][m][n] = max(take, skip);\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n this->strs = strs;\n memset(memo, -1, sizeof(memo));\n return sol(0, m, n);\n }\n};", "memory": "120300" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<vector<short>> cache(strs.size(), vector<short>(10101, -1)); \n\n return findMaxForm(0, strs, cache, m, n);\n }\nprivate:\n short findMaxForm(int current, vector<string>& strs, vector<vector<short>>& cache, int m, int n) {\n if (m < 0 || n < 0) {\n return -1;\n }\n\n if (current >= strs.size()) {\n return 0;\n }\n\n short& result = cache[current][m*100+n];\n\n if (result != -1) {\n return result;\n }\n\n auto [zeroes, ones] = calcBits(strs[current]);\n\n result = max<short>(findMaxForm(current+1, strs, cache, m, n),\n 1 + findMaxForm(current+1, strs, cache, m-zeroes, n-ones));\n\n return result;\n }\n\n pair<int,int> calcBits(const string& str) {\n int zeroes = 0;\n int ones = 0;\n \n for (char c: str) {\n if (c == '0') {\n ++zeroes;\n } else {\n ++ones;\n }\n }\n\n return {zeroes, ones};\n }\n}; ", "memory": "120300" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<vector<short>> cache(strs.size(), vector<short>(10101, -1)); \n\n return findMaxForm(0, strs, cache, m, n);\n }\nprivate:\n short findMaxForm(int current, vector<string>& strs, vector<vector<short>>& cache, int m, int n) {\n if (current >= strs.size()) {\n return 0;\n }\n\n short& result = cache[current][m*100+n];\n\n if (result != -1) {\n return result;\n }\n\n auto [zeroes, ones] = calcBits(strs[current]);\n\n result = findMaxForm(current+1, strs, cache, m, n);\n\n if (m >= zeroes && n >= ones) {\n result = max<short>(result, 1 + findMaxForm(current+1, strs, cache, m-zeroes, n-ones));\n }\n\n return result;\n }\n\n pair<int,int> calcBits(const string& str) {\n int zeroes = 0;\n int ones = 0;\n \n for (char c: str) {\n if (c == '0') {\n ++zeroes;\n } else {\n ++ones;\n }\n }\n\n return {zeroes, ones};\n }\n}; ", "memory": "122646" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void count(vector<string>& strs,vector<pair<int,int>>& pq,int i){\n if(i>=strs.size()){\n return ;\n }\n string s=strs[i];\n int zeroes=0;\n int ones=0;\n for(int j=0;j<s.length();j++){\n if(s[j]=='0'){\n zeroes++;\n }\n else{\n ones++;\n }\n }\n pq.push_back({zeroes,ones});\n count(strs,pq,i+1);\n \n }\n int solveusingmem(vector<string>& strs,int i,int m,int n,vector<pair<int,int>>& pq,vector<vector<vector<int>>>& dp){\n if(i>=strs.size()){\n return 0;\n }\n if(dp[i][m][n]!=-1){\n return dp[i][m][n];\n }\n\n int include=0;\n if(m-pq[i].first>=0 && n-pq[i].second>=0){\n include=1+solveusingmem(strs,i+1,m-pq[i].first,n-pq[i].second,pq,dp);\n }\n int exclude=solveusingmem(strs,i+1,m,n,pq,dp);\n dp[i][m][n]=max(include,exclude);\n return dp[i][m][n];\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<pair<int,int>>pq;\n for(int i=0;i<strs.size();i++){\n count(strs,pq,0);\n }\n int k=strs.size();\n vector<vector<vector<int>>>dp(k+1,vector<vector<int>>(m+1,vector<int>(n+1,-1)));\n int ans=solveusingmem(strs,0,m,n,pq,dp);\n return ans;\n }\n};", "memory": "124993" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxOne, maxZero;\n int dp[601][101][101];\n\n pair<int, int> findFreq(string& s){\n pair<int, int> p = {0, 0};\n\n for(auto it : s){\n if(it == '0') p.first++;\n else p.second++;\n }\n\n return p;\n }\n\n int solve(int ind, int zero, int one, vector<string>& v){\n\n if(ind >= v.size()) return 0;\n\n if(dp[ind][zero][one] != -1) return dp[ind][zero][one];\n\n pair<int, int> p = findFreq(v[ind]);\n\n vector<int> ans(2, INT_MIN);\n\n if(p.first + zero <= maxZero && p.second + one <= maxOne){\n ans[0] = 1 + solve(ind + 1, p.first + zero, p.second + one, v);\n }\n ans[1] = solve(ind + 1, zero, one, v);\n\n return dp[ind][zero][one] = max(ans[0], ans[1]);\n }\n\n int findMaxForm(vector<string>& strs, int m, int n) {\n \n maxOne = n;\n maxZero = m;\n memset(dp, -1, sizeof(dp));\n\n // dp[i][j][k]: max size of subset for all strings from i to end\n // such that total 0s <= maxZero, total 1s is maxOne\n\n return solve(0, 0, 0, strs);\n }\n};", "memory": "127339" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(vector<pair<int,int>> &zeroOnes,int m ,int n,int i){\n\n if(i >= zeroOnes.size())\n return 0 ;\n\n int include = 0 ;\n int currZero = zeroOnes[i].first ;\n int currOne = zeroOnes[i].second ;\n if(currZero <= m && currOne <= n)\n include = 1 + solve(zeroOnes,m-currZero,n-currOne,i+1);\n\n int exclude = 0 + solve(zeroOnes,m,n,i+1);\n\n return max(include,exclude);\n }\n\n int solveMem(vector<pair<int,int>> &zeroOnes,int m ,int n,int i, vector<vector<vector<int>>>& dp){\n\n if(i >= zeroOnes.size())\n return 0 ;\n\n if(dp[m][n][i] != -1)\n return dp[m][n][i];\n\n int include = 0 ;\n int currZero = zeroOnes[i].first ;\n int currOne = zeroOnes[i].second ;\n if(currZero <= m && currOne <= n)\n include = 1 + solveMem(zeroOnes,m-currZero,n-currOne,i+1,dp);\n\n int exclude = 0 + solveMem(zeroOnes,m,n,i+1,dp);\n\n return dp[m][n][i] = max(include,exclude);\n }\n\n int solveTab(vector<pair<int,int>> &zeroOnes,int m ,int n){\n vector<vector<vector<int>>> dp(m+1,vector<vector<int>>(n+1,vector<int>(zeroOnes.size()+1,0)));\n\n for(int i=0; i<=m; i++){\n for(int j=0; j<=n; j++){\n for(int index=zeroOnes.size()-1; index>=0; index--){\n\n int include = 0 ;\n int currZero = zeroOnes[index].first ;\n int currOne = zeroOnes[index].second ;\n if(currZero <= i && currOne <= j)\n include = 1 + dp[i-currZero][j-currOne][index+1];\n\n int exclude = 0 + dp[i][j][index+1];\n\n dp[i][j][index] = max(include,exclude);\n\n }\n }\n }\n\n return dp[m][n][0];\n\n }\n\n\n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<pair<int,int>> zeroOnes ; // {zero,ones};\n for(auto s:strs){\n \n int z = 0 , o = 0 ;\n for(int i=0; i<s.length(); i++)\n {\n if(s[i] == '0') z++;\n if(s[i] == '1') o++;\n }\n\n zeroOnes.push_back({z,o});\n }\n\n // int ans = solve(zeroOnes,m,n,0);\n int size = strs.size();\n vector<vector<vector<int>>> dp(m+1,vector<vector<int>>(n+1,vector<int>(size+1,-1)));\n // int ans = solveMem(zeroOnes,m,n,0,dp);\n int ans = solveTab(zeroOnes,m,n) ;\n return ans ;\n }\n};", "memory": "129685" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n\n int solveTab(vector<pair<int,int>> &zeroOnes,int m ,int n){\n vector<vector<vector<int>>> dp(m+1,vector<vector<int>>(n+1,vector<int>(zeroOnes.size()+1,0)));\n\n for(int i=0; i<=m; i++){\n for(int j=0; j<=n; j++){\n for(int index=zeroOnes.size()-1; index>=0; index--){\n\n int include = 0 ;\n int currZero = zeroOnes[index].first ;\n int currOne = zeroOnes[index].second ;\n if(currZero <= i && currOne <= j)\n include = 1 + dp[i-currZero][j-currOne][index+1];\n\n int exclude = 0 + dp[i][j][index+1];\n\n dp[i][j][index] = max(include,exclude);\n\n }\n }\n }\n\n return dp[m][n][0];\n\n }\n\n\n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<pair<int,int>> zeroOnes ; // {zero,ones};\n for(auto s:strs){\n \n int z = 0 , o = 0 ;\n for(int i=0; i<s.length(); i++)\n {\n if(s[i] == '0') z++;\n if(s[i] == '1') o++;\n }\n\n zeroOnes.push_back({z,o});\n }\n\n // int ans = solve(zeroOnes,m,n,0);\n int size = strs.size();\n vector<vector<vector<int>>> dp(m+1,vector<vector<int>>(n+1,vector<int>(size+1,-1)));\n // int ans = solveMem(zeroOnes,m,n,0,dp);\n int ans = solveTab(zeroOnes,m,n) ;\n return ans ;\n }\n};", "memory": "132031" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int dp[601][101][101]; \n int sub(vector<string>& strs, int n, int m, int idx){\n if( idx >= strs.size()) return 0;\n if (dp[idx][n][m] != -1) return dp[idx][n][m];\n vector<int> cnt = count(strs[idx]);\n\n int take = 0;\n if(n - cnt[0] >= 0 && m - cnt[1] >=0) take = 1 + sub(strs, n- cnt[0], m - cnt[1], idx+1);\n int nottake = sub(strs, n, m, idx+1);\n\n return dp[idx][n][m] = max(take, nottake);\n }\n vector<int> count(string s){\n vector<int> cnt(2, 0);\n\n for(int i=0; i<s.size(); i++){\n cnt[s[i] - '0']++;\n }\n return cnt;\n }\n int findMaxForm(vector<string>& strs, int n, int m) {\n memset(dp, -1, sizeof(dp)); \n return 1 + sub(strs, n , m, 0) -1;\n }\n};", "memory": "134378" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<vector<long>>> memo;\n int m, n;\n vector<string> strs;\n \n int findMaxForm(vector<string>& strs, int m, int n) {\n this->m = m;\n this->n = n; \n this->strs = strs;\n memo = vector<vector<vector<long>>>(strs.size(), vector<vector<long>>(m + 1, vector<long>(n + 1, -1)));\n return (int)dp(0, 0, 0);\n }\n \n long dp(int i, int currZeroes, int currOnes) {\n if(i >= strs.size()) {\n return 0;\n }\n \n if(memo[i][currZeroes][currOnes] == -1) {\n //Choice 1: Don't include current string in subset\n long option1 = dp(i+1, currZeroes, currOnes);\n \n //Choice 2: Include current string in subset\n pair<int,int> zeroOneCount = findZeroesAndOnes(strs[i]);\n if(currZeroes + zeroOneCount.first <= m && currOnes + zeroOneCount.second <= n) {\n // currZeroes += zeroOneCount.first; \n // currOnes += zeroOneCount.second;\n long option2 = 1 + dp(i+1, currZeroes + zeroOneCount.first, currOnes + zeroOneCount.second);\n memo[i][currZeroes][currOnes] = max(option1, option2);\n }\n \n else {\n memo[i][currZeroes][currOnes] = option1;\n }\n } \n \n return memo[i][currZeroes][currOnes];\n }\n \n pair<int,int> findZeroesAndOnes(string s) {\n pair<int,int> res = make_pair(0, 0);\n for(char c : s) {\n if(c == '0') res.first++;\n else res.second++;\n }\n return res;\n }\n \n};\n", "memory": "136724" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<vector<long>>> memo;\n int m, n;\n vector<string> strs;\n \n int findMaxForm(vector<string>& strs, int m, int n) {\n this->m = m;\n this->n = n; \n this->strs = strs;\n memo = vector<vector<vector<long>>>(strs.size(), vector<vector<long>>(m + 1, vector<long>(n + 1, -1)));\n return (int)dp(0, 0, 0);\n }\n \n long dp(int i, int currZeroes, int currOnes) {\n if(i >= strs.size()) {\n return 0;\n }\n \n if(memo[i][currZeroes][currOnes] == -1) {\n //Choice 1: Don't include current string in subset\n long option1 = dp(i+1, currZeroes, currOnes);\n \n //Choice 2: Include current string in subset\n pair<int,int> zeroOneCount = findZeroesAndOnes(strs[i]);\n if(currZeroes + zeroOneCount.first <= m && currOnes + zeroOneCount.second <= n) {\n // currZeroes += zeroOneCount.first; \n // currOnes += zeroOneCount.second;\n long option2 = 1 + dp(i+1, currZeroes + zeroOneCount.first, currOnes + zeroOneCount.second);\n memo[i][currZeroes][currOnes] = max(option1, option2);\n }\n \n else {\n memo[i][currZeroes][currOnes] = option1;\n }\n } \n \n return memo[i][currZeroes][currOnes];\n }\n \n pair<int,int> findZeroesAndOnes(string s) {\n pair<int,int> res = make_pair(0, 0);\n for(char c : s) {\n if(c == '0') res.first++;\n else res.second++;\n }\n return res;\n }\n \n};", "memory": "139070" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n \n int t[101][101][601];\n\n\n vector<int> countZeroOne(string str){\n \n \n\n vector<int>count(2);\n for(char c : str){\n\n count[c-'0']++;\n\n }\n\n return count;\n\n\n }\n \n int helper(vector<string>& strs,int m,int n,int index){\n \n \n\n\n if(index == strs.size() || (m+n) == 0){\n \n return 0;\n\n }\n\n if(t[m][n][index] != -1){\n \n return t[m][n][index];\n }\n vector<int>count = countZeroOne(strs[index]);\n\n int consider = 0;\n\n if(m >= count[0] && n >= count[1]){\n \n consider = 1 + helper(strs,m-count[0],n-count[1],index + 1);\n\n }\n\n int skip= helper(strs,m,n,index + 1);\n\n return t[m][n][index]= max(consider,skip);\n\n\n\n\n\n }\n\n int findMaxForm(vector<string>& strs, int m, int n) {\n \n memset(t,-1,sizeof(t));\n\n\n return helper(strs,m,n,0);\n\n }\n};", "memory": "141416" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic: \n\n void convertStrsToNums(vector<string>& strs , vector<pair<int , int> > &nums){\n for(auto str : strs){\n int zeros = 0;\n int ones = 0;\n\n for(auto s : str){\n if(s == '0') zeros++;\n else ones++;\n }\n nums.push_back({zeros , ones});\n }\n }\n\n int solveUsingRec(vector<pair<int, int> >& nums , int m , int n , int i){\n if(i >= nums.size()) return 0;\n if(m == 0 && n == 0) return 0;\n if(m < 0 || n < 0) return 0;\n\n //include:\n int size1 = 1 + solveUsingRec(nums , m-nums[i].first , n-nums[i].second , i+1);\n\n //exclude:\n int size2 = solveUsingRec(nums , m , n , i+1);\n\n int ans = max(size1 , size2);\n return ans;\n }\n\n\n int solveUsingMem(vector<pair<int, int> >& nums , int m , int n , int i , vector<vector<vector<int> > > &dp){\n if(i >= nums.size()) return 0;\n if(dp[m][n][i] != -1) return dp[m][n][i];\n\n //include:\n int size1 = INT_MIN;\n if(m-nums[i].first >= 0 && n-nums[i].second >= 0)\n size1 = 1 + solveUsingMem(nums , m-nums[i].first , n-nums[i].second , i+1 , dp);\n\n //exclude:\n int size2 = solveUsingMem(nums , m , n , i+1 , dp);\n\n int ans = max(size1 , size2);\n dp[m][n][i] = ans;\n return dp[m][n][i];\n }\n\n int solveUsingTab(vector<pair<int,int>>& nums , int m , int n){\n vector<vector<vector<int> > > dp(nums.size()+1 , vector<vector<int> >(m+1 , vector<int>(n+1 , 0)));\n\n for(int i = nums.size()-1 ; i >= 0 ; i--){\n for(int mIt = 0 ; mIt <= m ; mIt++){\n for(int nIt = 0 ; nIt <= n ; nIt++){\n int size1 = INT_MIN;\n int zeros = nums[i].first;\n int ones = nums[i].second;\n if(mIt-zeros >= 0 && nIt-ones >= 0)\n size1 = 1 + dp[i+1][mIt-nums[i].first][nIt-nums[i].second];\n\n //exclude:\n int size2 = dp[i+1][mIt][nIt];\n\n int ans = max(size1 , size2);\n dp[i][mIt][nIt] = ans;\n \n }\n }\n }\n return dp[0][m][n];\n }\n\n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<pair<int , int> > nums;\n convertStrsToNums(strs , nums);\n int size = nums.size();\n vector<vector<vector<int> > > dp(m+1 , vector<vector<int> >(n+1 , vector<int>(size+1 , -1)));\n\n return solveUsingTab(nums , m , n);\n }\n};", "memory": "143763" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int recursive(vector<string>& strs, int m, int n,int idx){\n if(idx >= strs.size()){\n return 0;\n }\n\n string s = strs[idx];\n\n int zeros = count(s.begin(),s.end(),'0');\n int ones = s.length() - zeros;\n\n int include = 0;\n \n if(m >= zeros && n >= ones){\n include = 1 + recursive(strs,m-zeros,n-ones,idx+1);\n }\n\n int exclude = recursive(strs,m,n,idx+1);\n\n return max(include,exclude);\n }\n\n int topdown(vector<string>& strs, int m, int n,int idx,vector<vector<vector<int>>>& dp){\n if(idx >= strs.size()){\n return 0;\n }\n\n if(dp[m][n][idx] != -1){\n return dp[m][n][idx];\n }\n\n string s = strs[idx];\n\n int zeros = 0;\n int ones = 0;\n for(int i=0;i<s.length();i++){\n if(s[i] == '0'){\n zeros++;\n }\n else{\n ones++;\n }\n }\n\n int include = 0;\n \n if(m >= zeros && n >= ones){\n include = 1 + topdown(strs,m-zeros,n-ones,idx+1,dp);\n }\n\n int exclude = topdown(strs,m,n,idx+1,dp);\n\n return dp[m][n][idx] = max(include,exclude);\n }\n\n int bottomup(vector<string>& strs, int m, int n){\n vector<vector<vector<int>>> dp(strs.size()+1,vector<vector<int>>(m+1,vector<int>(n+1,0)));\n\n for(int idx=strs.size()-1;idx>=0;idx--){\n for(int j=0;j<=m;j++){\n for(int k=0;k<=n;k++){\n string s = strs[idx];\n\n int zeros = 0;\n int ones = 0;\n for(int i=0;i<s.length();i++){\n if(s[i] == '0'){\n zeros++;\n }\n else{\n ones++;\n }\n }\n\n int include = 0;\n \n if(j >= zeros && k >= ones){\n include = 1 + dp[idx+1][j-zeros][k-ones];\n }\n\n int exclude = dp[idx+1][j][k];\n\n dp[idx][j][k] = max(include,exclude);\n }\n }\n }\n\n return dp[0][m][n];\n }\n\n int findMaxForm(vector<string>& strs, int m, int n) {\n // return recursive(strs,m,n,0);\n\n vector<vector<vector<int>>> dp(m+1,vector<vector<int>>(n+1,vector<int>(strs.size()+1,-1)));\n // return topdown(strs,m,n,0,dp);\n\n return bottomup(strs,m,n);\n }\n};", "memory": "146109" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int recursive(vector<string>& strs, int m, int n,int idx){\n if(idx >= strs.size()){\n return 0;\n }\n\n string s = strs[idx];\n\n int zeros = count(s.begin(),s.end(),'0');\n int ones = s.length() - zeros;\n\n int include = 0;\n \n if(m >= zeros && n >= ones){\n include = 1 + recursive(strs,m-zeros,n-ones,idx+1);\n }\n\n int exclude = recursive(strs,m,n,idx+1);\n\n return max(include,exclude);\n }\n\n int topdown(vector<string>& strs, int m, int n,int idx,vector<vector<vector<int>>>& dp){\n if(idx >= strs.size()){\n return 0;\n }\n\n if(dp[m][n][idx] != -1){\n return dp[m][n][idx];\n }\n\n string s = strs[idx];\n\n int zeros = 0;\n int ones = 0;\n for(int i=0;i<s.length();i++){\n if(s[i] == '0'){\n zeros++;\n }\n else{\n ones++;\n }\n }\n\n int include = 0;\n \n if(m >= zeros && n >= ones){\n include = 1 + topdown(strs,m-zeros,n-ones,idx+1,dp);\n }\n\n int exclude = topdown(strs,m,n,idx+1,dp);\n\n return dp[m][n][idx] = max(include,exclude);\n }\n\n int bottomup(vector<string>& strs, int m, int n){\n vector<vector<vector<int>>> dp(strs.size()+1,vector<vector<int>>(m+1,vector<int>(n+1,0)));\n\n for(int idx=strs.size()-1;idx>=0;idx--){\n for(int j=0;j<=m;j++){\n for(int k=0;k<=n;k++){\n string s = strs[idx];\n\n int zeros = 0;\n int ones = 0;\n for(int i=0;i<s.length();i++){\n if(s[i] == '0'){\n zeros++;\n }\n else{\n ones++;\n }\n }\n\n int include = 0;\n \n if(j >= zeros && k >= ones){\n include = 1 + dp[idx+1][j-zeros][k-ones];\n }\n\n int exclude = dp[idx+1][j][k];\n\n dp[idx][j][k] = max(include,exclude);\n }\n }\n }\n\n return dp[0][m][n];\n }\n\n int findMaxForm(vector<string>& strs, int m, int n) {\n // return recursive(strs,m,n,0);\n\n vector<vector<vector<int>>> dp(m+1,vector<vector<int>>(n+1,vector<int>(strs.size()+1,-1)));\n // return topdown(strs,m,n,0,dp);\n\n return bottomup(strs,m,n);\n }\n};", "memory": "146109" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\n int size, base = 601, mod = 1e9+7;\n unordered_map<int, pair<int, int>> map;\n unordered_map<int, int> dp;\n\n int calculateHash(int idx, int m, int n) {\n return (1LL*idx*base*base+m*base+n)%mod; \n }\n\n int solve(int idx, int m, int n) {\n if (m < 0 || n < 0) return INT_MIN;\n if (idx == size) return 0;\n int hash = calculateHash(idx, m, n);\n if (dp.find(hash) != dp.end()) return dp[hash];\n\n return dp[hash] = max(solve(idx+1, m, n), 1+solve(idx+1, m-map[idx].first, n-map[idx].second));\n }\npublic:\n int findMaxForm(vector<string>& strs, int m, int n) {\n size = strs.size();\n\n for (int i = 0; i < size; i++) {\n int z = 0, o = 0;\n for (char c: strs[i]) {\n if (c == '0') z++;\n else o++;\n }\n map[i] = {z, o};\n }\n\n return solve(0, m, n);\n }\n};", "memory": "148455" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int findMaxForm(vector<string>& strs, int zeros, int ones) {\n int n = strs.size();\n vector<pair<int,int>> counts;\n vector<vector<vector<int>>> dp(n);\n for(int i=0; i<n; i++){\n vector<vector<int>> state(zeros+1, vector<int>(ones+1));\n dp[i] = state;\n counts.push_back({\n count(strs[i].begin(), strs[i].end(), '0'),\n count(strs[i].begin(), strs[i].end(), '1')\n });\n }\n for(int i=0; i<n; i++){\n for(int zero=0; zero<=zeros; zero++){\n for(int one=0; one<=ones; one++){\n int strOnes, strZeros;\n strOnes = counts[i].second;\n strZeros = counts[i].first;\n\n if(one >= strOnes && zero >= strZeros){\n int include = (i > 0) ? 1 + dp[i-1][zero-strZeros][one-strOnes] : 1;\n int exclude = (i > 0) ? dp[i-1][zero][one] : 0;\n\n dp[i][zero][one] = max(include, exclude);\n }else{\n if(i != 0)\n dp[i][zero][one] = dp[i-1][zero][one];\n }\n }\n }\n }\n\n return dp[n-1][zeros][ones];\n }\n};", "memory": "150801" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector <pair<int,int>> arr;\n vector <vector <vector <int>>> dp;\n\n pair<int,int> f(string temp){\n\n int zero=0;\n int one=0;\n\n for(char c:temp){\n if(c=='0')\n zero++;\n else\n one++;\n }\n\n return {zero,one};\n }\n\n\n int f(int ind,int m,int n){\n\n if(ind==arr.size())\n return 0;\n\n if(dp[ind][m][n]!=-1)\n return dp[ind][m][n];\n\n if(m>=arr[ind].first && n>=arr[ind].second ){\n return dp[ind][m][n]=max(1+f(ind+1,m-arr[ind].first,n-arr[ind].second),f(ind+1,m,n));\n }\n else\n return dp[ind][m][n]=f(ind+1,m,n);\n \n }\n\n\n\n\n int findMaxForm(vector<string>& strs, int m, int n) {\n \n vector <vector <vector <int>>> dp2(strs.size(),vector <vector <int>> (m+1,vector <int> (n+1,-1)));\n dp=dp2;\n\n for(auto it: strs){\n arr.push_back(f(it));\n }\n return f(0,m,n);\n\n\n }\n};", "memory": "153148" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<string> strs;\n vector<int> count1;\n vector<int> count0;\n vector<vector<vector<int>>> dp;\n int dfs(int index, int zero, int one) {\n // end\n if (zero == 0 && one == 0)return 0;\n if (index >= strs.size() || one < 0 || zero < 0)return 0;\n // end_dp\n if(dp[index][zero][one])return dp[index][zero][one];\n\n int maximum = 0;\n\n int temp1 = 0;\n if (count1[index] <= one && count0[index] <= zero) {\n temp1 = dfs(index + 1, zero - count0[index], one - count1[index]) + 1;\n }\n\n int temp2 = dfs(index + 1, zero, one);\n\n maximum = max(temp1, temp2);\n dp[index][zero][one] = maximum;\n return maximum;\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n this->strs=strs;\n for(string s:strs){\n int cou1=0;\n int cou0=0;\n for(int i=0;i<s.size();i++){\n if(s[i]=='1')cou1++;\n else cou0++;\n }\n this->count1.push_back(cou1);\n this->count0.push_back(cou0);\n }\n \n vector<vector<vector<int>>> dp(strs.size(),vector<vector<int>>(m+1,vector<int>(n+1,0)));\n this->dp = dp;\n\n return dfs(0,m,n);\n }\n};", "memory": "155494" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(vector<string>& strs, int zero, int one, int i, vector<vector<vector<int>>> &dp){\n if(i>=strs.size() || zero+one==0){\n return 0;\n }\n\n if(dp[i][zero][one] != -1){\n return dp[i][zero][one];\n }\n\n// Count the number of zeros and ones in the current string\n int countZero = count(strs[i].begin(), strs[i].end(), '0');\n int countOne = strs[i].size() - countZero;\n\n int exc = solve(strs ,zero,one,i+1,dp);\n int inc = 0;\n if(zero >= countZero && one>=countOne){\n inc = 1 + solve(strs ,zero-countZero,one-countOne,i+1,dp);\n }\n \n return dp[i][zero][one]=max(exc,inc);\n }\n\n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<vector<vector<int>>> dp(strs.size()+1,vector<vector<int>>(m+1,vector<int>(n+1,-1)));\n vector<vector<vector<int>>> dp2(strs.size(), vector<vector<int>>(m + 1, vector<int>(n + 1, -1)));\n return solve(strs,m,n,0,dp);\n }\n};", "memory": "157840" }
474
<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p> <p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p> <p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4. Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}. {&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1 <strong>Output:</strong> 2 <b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= strs.length &lt;= 600</code></li> <li><code>1 &lt;= strs[i].length &lt;= 100</code></li> <li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= m, n &lt;= 100</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(vector<string>& strs, int m, int n, int i,vector<pair<int,int>> &count,vector<vector<vector<int>>> &dp){\n if(m < 0 || n < 0){\n return INT_MIN;\n }\n if(i == strs.size() ||(n == 0 && m == 0)){\n return 0;\n }\n if(dp[i][m][n] != -1){\n return dp[i][m][n];\n }\n int take = 1+solve(strs,m - count[i].first,n - count[i].second,i+1,count,dp);\n int nottake = solve(strs,m,n,i+1,count,dp);\n return dp[i][m][n] = max(take,nottake);\n\n }\n int tabuFindMax(vector<pair<int,int>>&strs,int m, int n)\n {\n vector<vector<vector<int>>>dp(strs.size()+1,vector<vector<int>>(m+1,vector<int>(n+1,0)));\n\n for(int i = strs.size()-1;i>=0;i--)\n {\n for( int j=0;j<=m;j++)\n {\n for(int k=0;k<=n;k++)\n {\n int zeroes = strs[i].first;\n int ones = strs[i].second;\n int include=0,exclude=0;\n\n if(j-zeroes >=0 && k-ones>=0)\n {\n include = 1 + dp[i+1][j-zeroes][k-ones];\n }\n exclude = dp[i+1][j][k];\n \n dp[i][j][k] = max(include,exclude);\n }\n }\n }\n\n return dp[0][m][n];\n }\n int findMaxForm(vector<string>& strs, int m, int n) {\n vector<pair<int,int>> count;\n vector<vector<vector<int>>> dp(strs.size(), vector<vector<int>>(m + 1, vector<int>(n + 1, -1)));\n for(int i = 0; i<strs.size();i++){\n int one = 0;\n int zero = 0;\n for(int j = 0; j<strs[i].size();j++){\n if(strs[i][j] == '0'){\n zero++;\n }\n if(strs[i][j] == '1'){\n one++;\n }\n }\n count.push_back({zero,one});\n }\n return tabuFindMax(count,m,n);\n }\n};", "memory": "160186" }
477
<p>The <a href="https://en.wikipedia.org/wiki/Hamming_distance" target="_blank">Hamming distance</a> between two integers is the number of positions at which the corresponding bits are different.</p> <p>Given an integer array <code>nums</code>, return <em>the sum of <strong>Hamming distances</strong> between all the pairs of the integers in</em> <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,14,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just showing the four bits relevant in this case). The answer will be: HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [4,14,4] <strong>Output:</strong> 4 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li>The answer for the given input will fit in a <strong>32-bit</strong> integer.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int totalHammingDistance(vector<int>& nums) {\n int total = 0;\n int n = nums.size();\n for (int i = 0; i < 32; ++i) {\n int countOnes = 0;\n for (int num : nums) {\n if (num & (1 << i)) {\n ++countOnes;\n }\n }\n int countZeros = n - countOnes;\n total += countOnes * countZeros;\n }\n \n return total;\n }\n};\n", "memory": "21500" }
477
<p>The <a href="https://en.wikipedia.org/wiki/Hamming_distance" target="_blank">Hamming distance</a> between two integers is the number of positions at which the corresponding bits are different.</p> <p>Given an integer array <code>nums</code>, return <em>the sum of <strong>Hamming distances</strong> between all the pairs of the integers in</em> <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,14,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just showing the four bits relevant in this case). The answer will be: HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [4,14,4] <strong>Output:</strong> 4 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li>The answer for the given input will fit in a <strong>32-bit</strong> integer.</li> </ul>
0
{ "code": "class Solution {\npublic:\n \n int totalHammingDistance(vector<int>& nums) {\n int size=nums.size();\n if(size<2) return 0;\n int ans=0;\n int* zerone=new int[2];\n while(true){\n int zerocount=0;\n zerone[0]=0;\n zerone[1]=0;\n for(int i=0;i<size;i++){\n if(nums[i]==0) zerocount++;\n zerone[nums[i]%2]++;\n nums[i]=nums[i]>>1;\n }\n ans+=zerone[0]*zerone[1];\n if(zerocount==nums.size()) return ans;\n }\n }\n};", "memory": "21500" }
477
<p>The <a href="https://en.wikipedia.org/wiki/Hamming_distance" target="_blank">Hamming distance</a> between two integers is the number of positions at which the corresponding bits are different.</p> <p>Given an integer array <code>nums</code>, return <em>the sum of <strong>Hamming distances</strong> between all the pairs of the integers in</em> <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,14,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just showing the four bits relevant in this case). The answer will be: HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [4,14,4] <strong>Output:</strong> 4 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li>The answer for the given input will fit in a <strong>32-bit</strong> integer.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int totalHammingDistance(vector<int>& nums) {\n int total = 0;\n int n = nums.size();\n for (int i = 0; i < 32; ++i) {\n int countOnes = 0;\n for (int num : nums) {\n if (num & (1 << i)) {\n ++countOnes;\n }\n }\n int countZeros = n - countOnes;\n total += countOnes * countZeros;\n }\n \n return total;\n }\n};\n", "memory": "21600" }
477
<p>The <a href="https://en.wikipedia.org/wiki/Hamming_distance" target="_blank">Hamming distance</a> between two integers is the number of positions at which the corresponding bits are different.</p> <p>Given an integer array <code>nums</code>, return <em>the sum of <strong>Hamming distances</strong> between all the pairs of the integers in</em> <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,14,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just showing the four bits relevant in this case). The answer will be: HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [4,14,4] <strong>Output:</strong> 4 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li>The answer for the given input will fit in a <strong>32-bit</strong> integer.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int totalHammingDistance(vector<int>& nums) {\n int t=0;\n int n=nums.size();\n\n for(int i=31;i>=0;i--)\n {\n int a=0;\n\n for(int j : nums)\n if(1&(j >> i))\n a++;\n \n t+=a*(n-a);\n }\n\n return t;\n }\n};", "memory": "21600" }
477
<p>The <a href="https://en.wikipedia.org/wiki/Hamming_distance" target="_blank">Hamming distance</a> between two integers is the number of positions at which the corresponding bits are different.</p> <p>Given an integer array <code>nums</code>, return <em>the sum of <strong>Hamming distances</strong> between all the pairs of the integers in</em> <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,14,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just showing the four bits relevant in this case). The answer will be: HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [4,14,4] <strong>Output:</strong> 4 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li>The answer for the given input will fit in a <strong>32-bit</strong> integer.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int totalHammingDistance(vector<int>& nums) {\n int n = nums.size();\n long long totalSum = 0;\n \n // Iterate over each bit position from 0 to 31\n for (int bit = 0; bit < 32; ++bit) {\n int countOnes = 0;\n \n // Count number of integers with the current bit set\n for (int num : nums) {\n if (num & (1 << bit)) {\n countOnes++;\n }\n }\n \n // Count of integers with the current bit not set\n int countZeros = n - countOnes;\n \n // Contribution of this bit to the total Hamming distance\n long long contribution = (long long)countOnes * countZeros;\n totalSum += contribution;\n }\n \n return static_cast<int>(totalSum);\n }\n};", "memory": "21700" }
477
<p>The <a href="https://en.wikipedia.org/wiki/Hamming_distance" target="_blank">Hamming distance</a> between two integers is the number of positions at which the corresponding bits are different.</p> <p>Given an integer array <code>nums</code>, return <em>the sum of <strong>Hamming distances</strong> between all the pairs of the integers in</em> <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,14,2] <strong>Output:</strong> 6 <strong>Explanation:</strong> In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just showing the four bits relevant in this case). The answer will be: HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [4,14,4] <strong>Output:</strong> 4 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li>The answer for the given input will fit in a <strong>32-bit</strong> integer.</li> </ul>
1
{ "code": "class Solution {\n public:\n int totalHammingDistance(vector<int>& nums) {\n constexpr int kMaxMask = 1 << 30;\n int ans = 0;\n\n for (int mask = 1; mask < kMaxMask; mask <<= 1) {\n const int ones =\n ranges::count_if(nums, [mask](int num) { return num & mask; });\n const int zeros = nums.size() - ones;\n ans += ones * zeros;\n }\n\n return ans;\n }\n};", "memory": "21700" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool track(ListNode *head, TreeNode *root) {\n if (!head) return true; \n if (!root) return false;\n\n if (head->val == root->val)\n if (track(head->next, root->left) || track(head->next, root->right))\n return true;\n \n return false;\n }\n\n bool isSubPath(ListNode* head, TreeNode* root) {\n if (!head) return true; // An empty list is trivially a subpath\n if (!root) return false; // Tree is empty but list is not\n\n if (track(head, root))\n return true;\n\n auto res = isSubPath(head, root->left) || isSubPath(head, root->right);\n root->left = root->right = nullptr;\n \n return res;\n }\n};", "memory": "24800" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isSubPath(ListNode* head, TreeNode* root) {\n const uint64_t factor = 127;\n uint64_t mul = 1, target = 0;\n int len = 0;\n for (auto node = head; node; node = node->next) {\n target += node->val * mul;\n mul *= factor;\n ++len;\n }\n \n vector<TreeNode*> leaves;\n auto reverse = [&](auto self, auto node, auto parent) -> void {\n if (!node->left && !node->right)\n leaves.push_back(node);\n if (auto left = exchange(node->left, parent))\n self(self, left, node);\n if (node->right)\n self(self, node->right, node);\n };\n reverse(reverse, root, nullptr);\n \n bool success = false;\n for (auto leaf : leaves) {\n int depth = 0;\n uint64_t hash = 0;\n \n auto node = leaf;\n for (; node; node = node->left) {\n hash = hash * factor + node->val;\n if (++depth == len) break;\n }\n if (!node) continue;\n if (hash == target) {\n success = true;\n break;\n }\n \n while (node = node->left) {\n hash = hash * factor - leaf->val * mul + node->val;\n if (hash == target) {\n success = true;\n break;\n }\n leaf = leaf->left;\n }\n if (success) break;\n }\n \n root->left = nullptr;\n root->right = nullptr;\n return success;\n }\n};", "memory": "26000" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool isSubPath(ListNode* head, TreeNode* root, bool in_start = true) {\n if(head->val == root->val){\n if(head->next == nullptr) return true;\n\n if(root->left && isSubPath(head->next, root->left, false)) return true;\n if(root->right && isSubPath(head->next, root->right, false)) return true;\n\n }\n if(in_start){\n if(root->left && isSubPath(head, root->left)) return true;\n if(root->right && isSubPath(head, root->right)) return true;\n\n root->left = nullptr;\n root->right = nullptr;\n }\n return false;\n }\n};", "memory": "28900" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool check(ListNode* head, TreeNode* root){\n if(head->val!=root->val) return false;\n if(head->next==NULL) return true;\n bool ans = false;\n if(root->left) ans= ans|| check(head->next, root->left);\n if(root->right) ans= ans|| check(head->next,root->right);\n return ans;\n }\n bool dfs(ListNode* head, TreeNode* root){\n if(root==NULL) return false;\n bool ans = false;\n if(check(head,root)) return true;\n if(root->left) ans = ans||dfs(head,root->left);\n if(root->right) ans= ans||dfs(head,root->right);\n return ans;\n }\n bool isSubPath(ListNode* head, TreeNode* root) {\n return dfs(head,root);\n }\n};", "memory": "30400" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic: \n\n void helper(ListNode *head, TreeNode *root, bool &flag){\n if(head == NULL){\n flag = true;\n return;\n }\n if(root == NULL){\n return;\n }\n if(head->val == root->val){\n helper(head->next, root->left, flag);\n helper(head->next, root->right, flag);\n }\n \n }\n void solve(ListNode *head, TreeNode *root, bool &flag){\n if(!head){\n flag = true;\n return;\n }\n if(!root){\n return;\n }\n if(head->val == root->val){\n helper(head->next, root->left, flag);\n helper(head->next, root->right, flag);\n }\n solve(head, root->left, flag);\n solve(head, root->right, flag);\n }\n bool isSubPath(ListNode* head, TreeNode* root) {\n bool flag = false;\n solve(head, root, flag);\n return flag;\n }\n};", "memory": "30400" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool check(ListNode* head, TreeNode* root)\n {\n if(!head) return true;\n if(!root) return false;\n if(head->val == root->val) \n {\n return check(head->next , root->left) || check(head->next , root->right);\n }\n return false;\n }\n bool isSubPath(ListNode* head, TreeNode* root) {\n if(!head) return 1;\n if(root==NULL) return false;\n if(check(head , root)) return true;\n return isSubPath(head , root->left) || isSubPath(head , root->right); \n }\n};", "memory": "30500" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n \n bool dfs(ListNode* head, TreeNode* root){\n if(!head) return true;\n if(!root) return false;\n\n if(root->val != head->val){\n return false;\n }\n else return dfs(head->next, root->left) || dfs(head->next, root->right);\n }\n \n \n \n bool isSubPath(ListNode* head, TreeNode* root) {\n \n // linkedlist order matters in the path in the tree\n\n if(!root) return false;\n if(!head) return false;\n\n return dfs(head, root) || isSubPath(head, root->left) || isSubPath(head,root->right);\n\n\n \n\n\n\n\n\n\n }\n};", "memory": "30600" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool a = false;\n bool isSubPath(ListNode* head, TreeNode* root) {\n help(head,root);\n return a;\n }\n void help(ListNode* head,TreeNode* root){\n if(head == NULL){\n a=true;\n return;\n }\n if(root == NULL){\n return;\n }\n if(head->val==root->val){\n helper(head->next,root->left);\n helper(head->next,root->right);\n }\n help(head,root->left);\n help(head,root->right);\n }\n void helper(ListNode* head,TreeNode* root){\n if(head == NULL){\n a=true;\n return;\n }\n if(root == NULL){\n return;\n }\n if(head->val==root->val){\n helper(head->next,root->left);\n helper(head->next,root->right);\n }\n }\n};", "memory": "30600" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
0
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isRootSubPath(ListNode* head, TreeNode* root){\n if(!head) return true;\n else if(!root) return false;\n else if(head->val == root->val){\n return isRootSubPath(head->next, root->left) \n || isRootSubPath(head->next, root->right);\n }\n else return false;\n }\n bool isSubPath(ListNode* head, TreeNode* root) {\n if(isRootSubPath(head, root)) return true;\n else if(root){\n return isSubPath(head, root->left) || isSubPath(head, root->right);\n } else return false;\n }\n};", "memory": "30700" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
0
{ "code": "class Solution {\n public:\n bool isSubPath(ListNode* head, TreeNode* root) {\n if (root == nullptr)\n return false;\n return isContinuousSubPath(head, root) || isSubPath(head, root->left) ||\n isSubPath(head, root->right);\n }\n\n private:\n bool isContinuousSubPath(ListNode* head, TreeNode* root) {\n if (head == nullptr)\n return true;\n if (root == nullptr)\n return false;\n return head->val == root->val &&\n (isContinuousSubPath(head->next, root->left) ||\n isContinuousSubPath(head->next, root->right));\n }\n};", "memory": "30700" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isSubPathNonSkip(ListNode* head, TreeNode* root) {\n if (head == nullptr)\n return true;\n if (root == nullptr)\n return false;\n if (head->val != root->val)\n return false;\n\n return isSubPathNonSkip(head->next, root->left) || isSubPathNonSkip(head->next, root->right);\n }\n bool isSubPath(ListNode* head, TreeNode* root) {\n if (head == nullptr)\n return true;\n if (root == nullptr)\n return false;\n\n return isSubPath(head, root->left) || isSubPath(head, root->right) || isSubPathNonSkip(head, root);\n }\n};", "memory": "30800" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
1
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool check(ListNode* head, TreeNode* root)\n {\n if(head==NULL)return true;\n if(root==NULL)return false;\n \n return head->val==root->val&& (check(head->next,root->left)||check(head->next,root->right));\n }\n bool isSubPath(ListNode* head, TreeNode* root) {\n if(head==NULL)return true;\n if(root==NULL)return false;\n \n return check(head,root)||isSubPath(head,root->left)||isSubPath(head,root->right);\n }\n};", "memory": "30800" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isLLPresent(TreeNode* root, ListNode *head){\n if(head == NULL){\n return true;\n }\n if(root == NULL || root->val != head->val){\n return false;\n }\n return isLLPresent(root->left, head->next) || isLLPresent(root->right, head->next);\n }\n bool isSubPath(ListNode* head, TreeNode* root) {\n if(root == NULL){\n return false;\n }\n if(isLLPresent(root, head)){\n return true;\n }else{\n return isSubPath(head, root->left) || isSubPath(head, root->right);\n }\n }\n};", "memory": "30900" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
2
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool find_path(TreeNode* root, ListNode* head) {\n // cout << root->val << \", \" << head->val << endl;\n \n if (!head->next) {\n return true; // We've reached the end of the linked list\n }\n \n if (!root) {\n return false; // We've reached a null node in the tree, but the list still has elements\n }\n\n bool left = false;\n if (root->left && root->left->val == head->next->val) {\n left = find_path(root->left, head->next); // Explore left subtree\n }\n \n bool right = false;\n if (root->right && root->right->val == head->next->val) {\n right = find_path(root->right, head->next); // Explore right subtree\n }\n\n return (left || right);\n }\n\n bool inorder(TreeNode* root, ListNode* head){\n if(!root){\n return false;\n }\n cout<<\"Traversal:\"<<root->val<<endl;\n bool left = inorder(root->left,head);\n bool root_path = false;\n if(root->val==head->val){\n root_path = find_path(root, head);\n }\n bool right = inorder(root->right, head);\n \n return (left||root_path||right);\n }\n bool isSubPath(ListNode* head, TreeNode* root) {\n if(!root&&!head){\n return true;\n }\n if(!root||!head){\n return false;\n }\n // cout<<head->val<<\",\"<<head->next->val<<endl;\n return inorder(root, head);\n }\n};", "memory": "30900" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool solve(ListNode* head, TreeNode* root,int f)\n {\n if(head==NULL) return 1;\n else if (root==NULL) return 0;\n if(head->val==root->val)\n {\n if(solve(head->next,root->left,0) || solve(head->next,root->right,0)) return 1;\n }\n if(f==1)\n {\n if(solve(head,root->left,f) || solve(head,root->right,f)) return 1; \n }\n return 0;\n // return solve(head)\n }\n bool isSubPath(ListNode* head, TreeNode* root) {\n return solve(head,root,1);\n }\n};", "memory": "31000" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool SubPath(ListNode* head, TreeNode* root, ListNode*start)\n {\n if(head==NULL)\n return true;\n else if(root==NULL)\n return false;\n if(head==start)\n {\n if(head->val==root->val)\n return SubPath(head->next,root->right,start)|SubPath(head,root->right,start)|SubPath(head,root->left,start)|SubPath(head->next,root->left,start);\n else\n return SubPath(head,root->right,start)|SubPath(head,root->left,start);\n }\n else\n {\n if(head->val!=root->val)\n return false;\n else\n return SubPath(head->next,root->right,start)|SubPath(head->next,root->left,start);\n }\n }\n bool isSubPath(ListNode* head, TreeNode* root) {\n ListNode*start=head;\n return SubPath(head,root,start);\n }\n};", "memory": "31000" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution\n{\nprivate:\n static constexpr bool endsWith(const std::vector<int>& range, const std::vector<int>& suffix)\n {\n if (range.size() < suffix.size())\n return false;\n\n for (auto rit = range.rbegin(), sit = suffix.rbegin();\n sit != suffix.rend();\n rit++, sit++)\n {\n if (*rit != *sit)\n return false;\n }\n return true;\n }\n\n static bool isSubPath(std::vector<int>& stack, const std::vector<int>& list, const TreeNode* node)\n {\n stack.push_back(node->val);\n const auto result = (node->val == list.back() && endsWith(stack, list))\n || (node->left != nullptr && isSubPath(stack, list, node->left))\n || (node->right != nullptr && isSubPath(stack, list, node->right));\n stack.pop_back();\n return result;\n }\n\npublic:\n bool isSubPath(const ListNode* head, const TreeNode* root) const\n {\n std::vector<int> list;\n for (auto curr = head; curr != nullptr; curr = curr->next)\n {\n list.push_back(curr->val);\n }\n std::vector<int> stack;\n return isSubPath(stack, list, root);\n }\n};\n", "memory": "31100" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "class Solution {\n bool traverseTree(std::vector<int> &kmpPrefix, std::vector<int> &listNums, TreeNode *node, int curIndex = 0) {\n if(curIndex == listNums.size()) return true;\n if(node) {\n // std::cout<<node->val<<\", \"<<listNums[curIndex]<<\", \"<<curIndex<<std::endl;\n if(node->val == listNums[curIndex]) {\n return traverseTree(kmpPrefix, listNums, node->right, curIndex + 1) ||\n traverseTree(kmpPrefix, listNums, node->left, curIndex + 1);\n }\n else {\n curIndex = kmpPrefix[std::max(0, curIndex - 1)];\n if(node->val == listNums[curIndex]) curIndex++;\n return traverseTree(kmpPrefix, listNums, node->right, curIndex) ||\n traverseTree(kmpPrefix, listNums, node->left, curIndex);\n }\n }\n return false;\n }\npublic:\n bool isSubPath(ListNode* head, TreeNode* root) {\n /**\n * Use KMP \"string\" matching to figure out when to restart matching\n */\n std::vector<int> listNums;\n while(head) {\n listNums.push_back(head->val);\n head = head->next;\n }\n std::vector<int> kmpPrefix(listNums.size(), 0);\n for(int i = 1; i < listNums.size(); i++) {\n if(listNums[i] == listNums[kmpPrefix[i - 1]]) kmpPrefix[i] = kmpPrefix[i - 1] + 1;\n else kmpPrefix[i] = 0;\n }\n return traverseTree(kmpPrefix, listNums, root);\n }\n};", "memory": "31200" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool isSubPath(ListNode* head, TreeNode* root) {\n queue<TreeNode*> mq{{root}};\n while (!mq.empty()) {\n auto node = mq.front();\n mq.pop();\n if (node) {\n if (checkUntil(head, node)) return true;\n if (node->left) mq.push(node->left);\n if (node->right) mq.push(node->right);\n }\n }\n return false;\n }\n\nprivate:\n bool checkUntil(ListNode* list_head, TreeNode* tree_root) {\n if (!list_head) return true;\n if (!tree_root) return false;\n \n if(tree_root->val == list_head->val) {\n return checkUntil(list_head->next, tree_root->left) || \n checkUntil(list_head->next, tree_root->right);\n } \n return false;\n }\n\n};", "memory": "31300" }
1,484
<p>Given a binary tree <code>root</code> and a&nbsp;linked list with&nbsp;<code>head</code>&nbsp;as the first node.&nbsp;</p> <p>Return True if all the elements in the linked list starting from the <code>head</code> correspond to some <em>downward path</em> connected in the binary tree&nbsp;otherwise return False.</p> <p>In this context downward path means a path that starts at some node and goes downwards.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_1_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true <strong>Explanation:</strong> Nodes in blue form a subpath in the binary Tree. </pre> <p><strong class="example">Example 2:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/sample_2_1720.png" style="width: 220px; height: 280px;" /></strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> true </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] <strong>Output:</strong> false <strong>Explanation:</strong> There is no path in the binary tree that contains all the elements of the linked list from <code>head</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree will be in the range <code>[1, 2500]</code>.</li> <li>The number of nodes in the list will be in the range <code>[1, 100]</code>.</li> <li><code>1 &lt;= Node.val&nbsp;&lt;= 100</code>&nbsp;for each node in the linked list and binary tree.</li> </ul>
3
{ "code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nbool flag = false;\nvoid recursive(TreeNode* check, ListNode* node) {\n if (node != nullptr) {\n if (check->left != nullptr && check->left->val == node->val) {\n recursive(check->left, node->next);\n }\n if (check->right != nullptr && check->right->val == node->val) {\n recursive(check->right, node->next);\n }\n } \n else {\n flag = true; return;\n }\n}\n bool isSubPath(ListNode* head, TreeNode* root) {\n queue<TreeNode*> paths, checks;\n paths.push(root);\n while (paths.size() != 0) {\n for (int i = paths.size() -1; i >= 0; paths.pop(), i--) {\n if (paths.front()->val == head->val) checks.push(paths.front());\n if (paths.front()->left != nullptr) paths.push(paths.front()->left);\n if (paths.front()->right != nullptr) paths.push(paths.front()->right);\n }\n }\n while (!checks.empty() && !flag) {\n recursive(checks.front(), head->next);\n checks.pop();\n }\n return flag;\n }\n};", "memory": "31700" }