id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumValueSum(vector<int>& a, vector<int>& b) {\n int n = a.size();\n int m = b.size();\n vector<map<int, int>> mp(m + 1);\n vector<vector<int>> dp(n + 1, vector<int>(m + 1, 1e9));\n dp[0][0] = 0;\n for (int i = 0; i < n; i++){\n for (int j = 0; j < m; j++){\n map<int, int> mm;\n mm[a[i]] = dp[i][j];\n for (auto [ii, jj] : mp[j])\n if (mm.count(ii & a[i]))\n mm[ii & a[i]] = min(mm[ii & a[i]], jj);\n else\n mm[ii & a[i]] = jj;\n mp[j] = mm;\n if (mp[j].count(b[j]))\n dp[i + 1][j + 1] = min(dp[i + 1][j + 1], mp[j][b[j]] + a[i]);\n }\n }\n if (dp[n][m] == 1e9)\n return -1;\n return dp[n][m];\n }\n};", "memory": "201492" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumValueSum(vector<int>& a, vector<int>& b) {\n int n = a.size();\n int m = b.size();\n vector<map<int, int>> mp(m + 1);\n vector<vector<int>> dp(n + 1, vector<int>(m + 1, 1e9));\n dp[0][0] = 0;\n for (int i = 0; i < n; i++){\n for (int j = 0; j < m; j++){\n map<int, int> mm;\n mm[a[i]] = dp[i][j];\n for (auto [ii, jj] : mp[j])\n if (mm.count(ii & a[i]))\n mm[ii & a[i]] = min(mm[ii & a[i]], jj);\n else\n mm[ii & a[i]] = jj;\n mp[j] = mm;\n if (mp[j].count(b[j]))\n dp[i + 1][j + 1] = min(dp[i + 1][j + 1], mp[j][b[j]] + a[i]);\n }\n }\n if (dp[n][m] == 1e9)\n return -1;\n return dp[n][m];\n }\n};", "memory": "201492" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int func(int i, int ind, vector<int>& nums, vector<int>& andV,vector<vector<map<int,int>>>&dp,int andop) {\n int n = nums.size();\n int m = andV.size();\n if(dp[i][ind].count(andop))return dp[i][ind][andop];\n if (i==n and ind == m) {\n return 0; // All segments found\n }\n if (i == n) {\n return 1e9; // Not enough elements to form the required segments\n }\n if (ind == m)return 1e9;\n\n int an = -1; // Initialize to all 1s for AND operation\n int ans = 1e9;\n andop&=nums[i];\n int take=1e9;\n int notake=1e9;\n if(andop==andV[ind]){\n take=nums[i]+func(i+1,ind+1,nums,andV,dp,(1<<20) -1);\n }\n notake=func(i+1,ind,nums,andV,dp,andop);\n\n \n return dp[i][ind][andop]=min(take,notake);\n }\n\n int minimumValueSum(vector<int>& nums, vector<int>& andV) {\n int m = andV.size();\n int n = nums.size();\n vector<vector<map<int,int>>> dp( n+1, vector< map<int,int> > (m+1));\n int result = func(0, 0, nums, andV,dp,(1<<20)-1);\n if(result==1e9)return -1;\n return result;\n }\n};", "memory": "207727" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<vector<map<int,int>>>dp;\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n=nums.size();\n int m=andValues.size();\n dp.resize(n,vector<map<int,int>>(m));\n int ans=find(nums,0,andValues,0,n,m,-1);\n if(ans>=1e8)ans=-1;\n return ans;\n }\n int find(vector<int>& nums,int i,vector<int>& av,int j,int &n,int &m,int a){\n if(i>=n && j>=m)return 0;\n if(j>=m || i>=n)return 1e8;\n if(dp[i][j].find(a)!=dp[i][j].end())return dp[i][j][a];\n // int ans=find(nums,i+1,av,j,n,m,a);\n // int ans=1e8;\n // for(int x=i;x<n;x++){\n // if(a==-1)a=nums[x];\n // else a&=nums[x];\n // if(a==av[j])ans=min(ans,nums[x]+find(nums,x+1,av,j+1,n,m,-1));\n // }\n if(a==-1)a=nums[i];\n else a&=nums[i];\n int ans=find(nums,i+1,av,j,n,m,a);\n if(a==av[j]){\n ans=min(ans,nums[i]+find(nums,i+1,av,j+1,n,m,-1)); \n // cout<<i<<\" \"<<j<<\" \"<<a<<\" \"<<ans<<endl; \n }\n return dp[i][j][a]=ans; \n }\n};", "memory": "213962" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n \n vector<vector<map<int,int>>>dp;\n long long solve(int ind , int ind2 , int curr , vector<int>&nums,vector<int>&req){\n if(ind2==req.size() && ind==nums.size()) return 0;\n if(ind2==req.size()) return INT_MAX;\n if(ind==nums.size()) return INT_MAX;\n\n if(dp[ind][ind2].find(curr)!=dp[ind][ind2].end()) return dp[ind][ind2][curr];\n\n long long ans = INT_MAX;\n\n int temp = (curr & nums[ind]);\n\n if(temp == req[ind2]) ans = min(ans , nums[ind] + solve(ind+1,ind2+1,-1,nums,req));\n\n if((req[ind2] & temp ) == req[ind2]) ans = min(ans , 0 + solve(ind+1,ind2,temp,nums,req ));\n\n return dp[ind][ind2][curr] = ans;\n\n\n }\n int minimumValueSum(vector<int>& nums, vector<int>& req) {\n dp.resize(nums.size() , vector<map<int,int>>(req.size()));\n long long ans = solve(0,0,-1,nums,req);\n if(ans >=INT_MAX) return -1;\n return ans;\n\n\n }\n};", "memory": "220197" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size(), m = andValues.size();\n\n unordered_map<long long, int> dp;\n auto dfs = [&] (auto&& dfs, long long i, long long j, long long and_) ->int {\n if(i == n) {\n return j == m ? 0 : 0x3f3f3f3f;\n }\n if(j == m) {\n return 0x3f3f3f3f;\n }\n long long mask = (i << 36) + (j << 32) + and_;\n if(dp.count(mask)) {\n return dp[mask];\n }\n and_ &= nums[i];\n int res = dfs(dfs, i+1, j, and_);\n if(and_ == andValues[j])\n res = min(res, dfs(dfs, i+1, j+1, -1) + nums[i]);\n dp[mask] = res;\n return res;\n };\n\n int ans = dfs(dfs, 0, 0, -1);\n return ans < 0x3f3f3f3f ? ans : -1;\n }\n};", "memory": "226432" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<vector<map<int,int>>>mpp;\n int f(vector<int>& nums, vector<int>& v,int i,int j,int curr){\n if(i==nums.size()){\n if(j==v.size()-1 && curr==v[j])return nums[i-1];\n return 1e9;\n }\n if(mpp[i][j].count(curr))return mpp[i][j][curr];\n \n int ans=f(nums,v,i+1,j,(curr & nums[i]));\n if(j<v.size() && curr==v[j]){\n if(i>=1)ans=min(ans,nums[i-1]+f(nums,v,i+1,j+1,nums[i]));\n }\n return mpp[i][j][curr]=ans;\n }\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n=nums.size(),m=andValues.size();\n mpp = vector<vector<map<int, int>>>(n, vector<map<int, int>>(m+1));\n int ans=f(nums,andValues,0,0,nums[0]);\n if(ans==1e9)return -1;\n return ans;\n }\n};", "memory": "232667" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<vector<map<int,int>>>mpp;\n int f(vector<int>& nums, vector<int>& v,int i,int j,int curr){\n if(i==nums.size()){\n if(j==v.size()-1 && curr==v[j])return nums[i-1];\n return 1e9;\n }\n if(mpp[i][j].find(curr)!=mpp[i][j].end())return mpp[i][j][curr];\n \n int ans=f(nums,v,i+1,j,(curr & nums[i]));\n if(j<v.size() && curr==v[j]){\n if(i>=1)ans=min(ans,nums[i-1]+f(nums,v,i+1,j+1,nums[i]));\n }\n return mpp[i][j][curr]=ans;\n }\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n=nums.size(),m=andValues.size();\n mpp = vector<vector<map<int, int>>>(n, vector<map<int, int>>(m+1));\n int ans=f(nums,andValues,0,0,nums[0]);\n if(ans==1e9)return -1;\n return ans;\n }\n};", "memory": "238902" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int f(vector<int>& nums, vector<int>& v,int i,int j,int curr,vector<vector<map<int,int>>>& dp){\n if(i==nums.size()){\n if(j==v.size()-1 && curr==v[j])return nums[i-1];\n return 1e9;\n }\n if(dp[i][j].find(curr)!=dp[i][j].end())return dp[i][j][curr];\n \n int ans=f(nums,v,i+1,j,(curr & nums[i]),dp);\n if(j<v.size() && curr==v[j]){\n if(i>=1)ans=min(ans,nums[i-1]+f(nums,v,i+1,j+1,nums[i],dp));\n }\n return dp[i][j][curr]=ans;\n }\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n=nums.size(),m=andValues.size();\n vector<vector<map<int,int>>>dp(n,vector<map<int,int>>(m+1));\n int ans=f(nums,andValues,0,0,nums[0],dp);\n if(ans==1e9)return -1;\n return ans;\n }\n};", "memory": "245137" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\nvector<vector<map<int,int>>>dp;\nint fun(vector<int>&nums,vector<int>&ad,int i,int j,int curand){\n if(i>=nums.size()){\n if(j>=ad.size())return 0;\n return 1e7;\n }\n\n if(j>=ad.size())return 1e7;\n if(dp[i][j].count(curand))return dp[i][j][curand];\n int bk=1e7;\n if((nums[i]&curand) == ad[j]){\n bk=nums[i]+fun(nums,ad,i+1,j+1,(1<<20)-1);\n }\n int cn=fun(nums,ad,i+1,j,(curand&nums[i]));\n return dp[i][j][curand]=min(bk,cn);\n}\n int minimumValueSum(vector<int>& nums, vector<int>& ad) {\n int n=nums.size();\n dp.resize(n,vector<map<int,int>>(10));\n int ans=fun(nums,ad,0,0,(1<<20)-1);\n if(ans>=1e7)return -1;\n return ans;\n }\n};", "memory": "251372" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\nconst int q=(1<<20)-1;\n int INF = 2e9;\n int func(int n, int m, int i, int j, vector<vector<map<int, int>>>& dp, int range, vector<int>& nums, vector<int>& andValues) {\n if(i==n) {\n if(j==m && range==q) return 0;\n return INF;\n }\n if (dp[i][j].find(range) != dp[i][j].end()) return dp[i][j][range];\n int take = INF;\n if (i < n && j < m && andValues[j] == (range&nums[i])) {\n take = nums[i] + func(n, m, i + 1, j + 1, dp, q, nums, andValues);\n }\n int take_pull = func(n, m, i + 1, j, dp, nums[i] & range, nums, andValues);\n\n return dp[i][j][range] = min(take, take_pull);\n }\n\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n // simpul dp, 2 segment trees -> for AND and min , 0(n*logn*logn)[mostly cant implement/TLE]\n // much better, vector<vector<map>> dp(n , vector<map>(m)))\n // BECAUSE AT MOST 30 DIFFERENT VALUES OF \"andVal\"**\n \n int n = nums.size();\n int m = andValues.size();\n int range = (1 << 20) - 1; \n vector<vector<map<int, int>>> dp(n, vector<map<int, int>>(m+1));\n int ans = func(n, m, 0, 0, dp, range, nums, andValues);\n if(ans==INF) return -1;\n return ans;\n }\n};", "memory": "257607" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\nconst int q=(1<<20)-1;\n int INF = 2e9;\n int func(int n, int m, int i, int j, vector<vector<map<int, int>>>& dp, int range, vector<int>& nums, vector<int>& andValues) {\n if(i==n) {\n if(j==m && range==q) return 0;\n return INF;\n }\n if (dp[i][j].find(range) != dp[i][j].end()) return dp[i][j][range];\n int take = INF;\n if (i < n && j < m && andValues[j] == (range&nums[i])) {\n take = nums[i] + func(n, m, i + 1, j + 1, dp, q, nums, andValues);\n }\n int take_pull = func(n, m, i + 1, j, dp, nums[i] & range, nums, andValues);\n return dp[i][j][range] = min(take, take_pull);\n }\n\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size();\n int m = andValues.size();\n int range = (1 << 20) - 1; \n vector<vector<map<int, int>>> dp(n, vector<map<int, int>>(m+1));\n int ans = func(n, m, 0, 0, dp, range, nums, andValues);\n if(ans==INF) return -1;\n return ans;\n }\n};", "memory": "263842" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size(), m = andValues.size();\n memo.resize(m * n);\n int res = dfs(0, 0, INF, nums, andValues);\n return res < INF ? res : -1;\n }\n\nprivate:\n static const int INF = (1 << 20) - 1;\n vector<unordered_map<int, int>> memo;\n int dfs(int i, int j, int cur, vector<int>& nums, vector<int>& andValues) {\n int n = nums.size(), m = andValues.size(), key = i * m + j;\n if (i == n && j == m) return 0;\n if (i == n || j == m) return INF;\n if (memo[key].count(cur)) return memo[key][cur];\n\n cur &= nums[i];\n if ((cur & andValues[j]) < andValues[j]) return INF;\n\n int res = dfs(i + 1, j, cur, nums, andValues);\n if (cur == andValues[j]) {\n res = min(res, dfs(i + 1, j + 1, INF, nums, andValues) + nums[i]);\n }\n memo[key][cur] = res;\n return res;\n }\n};", "memory": "270077" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\n\n vector<vector<unordered_map<int, int>>>dp;\n\n long long helper(vector<int>&nums, vector<int>&arr, int ind = 0, int curr_and = (1 << 20) - 1, int group_num = 0) {\n if (ind == nums.size()) {\n if (group_num == (arr.size() - 1) && curr_and == arr.back()) return nums.back();\n return INT_MAX;\n }\n if (group_num >= (arr.size())) return INT_MAX;\n if (dp[ind][group_num].find(curr_and) != dp[ind][group_num].end()) return dp[ind][group_num][curr_and];\n curr_and &= nums[ind];\n long long res = INT_MAX;\n if (curr_and == arr[group_num]) res = min(res, (long long)nums[ind] + (long long)helper(nums, arr, ind+1, (1 << 20) - 1, group_num+1));\n res = min(res, helper(nums, arr, ind+1, curr_and, group_num));\n return dp[ind][group_num][curr_and] = res;\n }\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n dp = vector<vector<unordered_map<int, int>>>(nums.size(), vector<unordered_map<int, int>>(andValues.size()));\n int res = helper(nums, andValues);\n return res >= INT_MAX ? -1: res;\n }\n};", "memory": "276312" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n m = nums.size(), n = andValues.size();\n dp = vector<unordered_map<int, int>>(m * n);\n int ans = dfs(0, 0, -1, nums, andValues);\n return ans < INT_MAX / 2 ? ans : -1;\n }\nprivate:\n int dfs(int i, int j, int mask, vector<int>& nums, vector<int>& add_values) {\n if (i == m && j == n) {\n return 0;\n }\n if (i == m || j == n) {\n return INT_MAX / 2;\n }\n if (m - i < n - j) { // 剩余的数不足划分, 需要注意这个优化\n return INT_MAX / 2;\n }\n int key = i * n + j;\n if (dp[key].count(mask)) {\n return dp[key][mask];\n }\n // 如果nums[i]不是新段的最后一个元素\n mask &= nums[i];\n if (mask < add_values[j]) {\n return INT_MAX / 2;\n }\n int ans = dfs(i + 1, j, mask, nums, add_values);\n // 如果nums[i]作为新段的最后一个元素\n if (mask == add_values[j]) {\n ans = min(ans, dfs(i + 1, j + 1, -1, nums, add_values) + nums[i]);\n }\n return dp[key][mask] = ans;\n }\n int m, n;\n vector<unordered_map<int, int>> dp;\n};", "memory": "282547" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int solve(int i, int j, int val, vector<int>& nums, vector<int>& andValues, vector<vector<unordered_map<int, int>>>& dp) {\n if (i >= nums.size()) {\n if (j >= andValues.size()) return 0;\n else return 1000001;\n }\n if (j >= andValues.size()) return 1000001;\n\n if (dp[i][j].count(val)) return dp[i][j][val];\n\n val = val & nums[i];\n\n int ans = INT_MAX;\n if (val == andValues[j]) {\n ans = nums[i] + min(ans, solve(i + 1, j + 1, -1, nums, andValues, dp));\n ans = min(ans, solve(i + 1, j, val, nums, andValues, dp));\n } else {\n ans = min(ans, solve(i + 1, j, val, nums, andValues, dp));\n }\n\n return dp[i][j][val] = ans;\n }\n\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size();\n int m = andValues.size();\n if (m > n) return -1;\n \n // Initialize dp with default unordered_map\n vector<vector<unordered_map<int, int>>> dp(n + 1, vector<unordered_map<int, int>>(m + 1));\n\n int ans = solve(0, 0, -1, nums, andValues, dp);\n return ans > 1000000 ? -1 : ans;\n }\n};", "memory": "288782" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int solve(int i, int j, int val, vector<int>& nums, vector<int>& andValues, vector<vector<unordered_map<int, int>>>& dp) {\n if (i >= nums.size()) {\n if (j >= andValues.size()) return 0;\n else return 1000001;\n }\n if (j >= andValues.size()) return 1000001;\n\n if (dp[i][j].count(val)) return dp[i][j][val];\n\n if (val == -1) val = nums[i];\n else val = val & nums[i];\n\n int ans = INT_MAX;\n if (val == andValues[j]) {\n ans = nums[i] + min(ans, solve(i + 1, j + 1, -1, nums, andValues, dp));\n ans = min(ans, solve(i + 1, j, val, nums, andValues, dp));\n } else {\n ans = min(ans, solve(i + 1, j, val, nums, andValues, dp));\n }\n\n return dp[i][j][val] = ans;\n }\n\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size();\n int m = andValues.size();\n if (m > n) return -1;\n \n // Initialize dp with default unordered_map\n vector<vector<unordered_map<int, int>>> dp(n + 1, vector<unordered_map<int, int>>(m + 1));\n\n int ans = solve(0, 0, -1, nums, andValues, dp);\n return ans > 1000000 ? -1 : ans;\n }\n};", "memory": "295017" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "#include <vector>\n#include <unordered_map>\n#include <algorithm>\n\nclass Solution {\nprivate:\n std::unordered_map<int, long long> mp[10009][11];\n std::vector<int> a, an;\n int n, m;\n \npublic:\n long long solve(int i, int j, int nd) {\n // Base cases\n if (i == n && j == m) return 0;\n if (i == n || j == m) return 1e10;\n\n // Check memoization\n if (mp[i][j].count(nd)) return mp[i][j][nd];\n \n // Update nd with the current element\n if(nd!=-1)\n nd &= a[i];\n else\n nd=a[i];\n \n mp[i][j][nd] = solve(i + 1, j, nd); // Case where current element is not included in the subarray\n\n \n if (nd == an[j]) {\n mp[i][j][nd] = std::min(solve(i + 1, j + 1, -1) + a[i], mp[i][j][nd]);\n }\n \n return mp[i][j][nd];\n }\n \n int minimumValueSum(std::vector<int>& a, std::vector<int>& an) {\n this->a = a;\n this->an = an;\n n = a.size();\n m = an.size();\n \n long long ans = solve(0, 0, -1);\n return ans < 1e10 ? ans : -1;\n }\n};\n", "memory": "301252" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n \n vector<vector<unordered_map<int,int>>>dp;\n long long solve(int ind , int ind2 , int curr , vector<int>&nums,vector<int>&req){\n if(ind2==req.size() && ind==nums.size()) return 0;\n if(ind2==req.size()) return INT_MAX;\n if(ind==nums.size()) return INT_MAX;\n\n if(dp[ind][ind2].find(curr)!=dp[ind][ind2].end()) return dp[ind][ind2][curr];\n\n long long ans = INT_MAX;\n\n int temp = (curr & nums[ind]);\n\n if(temp == req[ind2]) ans = min(ans , nums[ind] + solve(ind+1,ind2+1,-1,nums,req));\n\n if((req[ind2] & temp ) == req[ind2]) ans = min(ans , 0 + solve(ind+1,ind2,temp,nums,req ));\n\n return dp[ind][ind2][curr] = ans;\n\n\n }\n int minimumValueSum(vector<int>& nums, vector<int>& req) {\n dp.resize(nums.size() , vector<unordered_map<int,int>>(req.size()));\n long long ans = solve(0,0,-1,nums,req);\n if(ans >=INT_MAX) return -1;\n return ans;\n\n\n }\n};", "memory": "307487" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n bool chk(int a, int b)\n {\n for (int i = 0; i < 32; i++) {\n int val = (1<<i);\n if (b&val && !(a&val)) {\n return false;\n }\n }\n \n return true;\n }\n int dfs(vector<int>& nums, vector<int>& values, int idx, int val_idx, int an, vector<vector<unordered_map<int,int>>>& dp)\n {\n if (idx >= nums.size() && val_idx >= values.size()) {\n return 0;\n }\n if (idx >= nums.size() || val_idx >= values.size()) {\n return INT_MAX;\n }\n \n auto it = dp[idx][val_idx].find(an);\n if (it != dp[idx][val_idx].end()) {\n return it->second;\n }\n \n int ans = INT_MAX;\n //cout << idx << \" \" << val_idx << \" \" << \"\";\n int temp = an&nums[idx];\n if (temp < values[val_idx]) {\n return INT_MAX;\n }\n //cout << temp << \"\\n\";\n if (temp == values[val_idx]) {\n //cout << idx << \" \" << val_idx << \"\\n\";\n int val = dfs(nums, values, idx+1, val_idx+1, (1<<17)-1, dp);\n if (val != INT_MAX) {\n ans = min(ans, nums[idx]+val);\n }\n }\n \n \n ans = min(ans, dfs(nums, values, idx+1, val_idx, an&nums[idx], dp));\n \n dp[idx][val_idx][an] = ans;\n return ans;\n }\n \n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n vector<vector<unordered_map<int,int>>> dp(nums.size(), vector<unordered_map<int,int>>(andValues.size()));\n int val = dfs(nums, andValues, 0, 0, (1<<17)-1, dp);\n return (val == INT_MAX) ? -1 : val;\n }\n};", "memory": "313722" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<vector<map<int,int>>>dp;\n int n,m;\n int f(int i,int j,int val,vector<int>&v,vector<int>&av){\n if(i>=n){\n if(j>=m){\n return 0;\n }\n return 1e9;\n }\n if(j>=m){\n return 1e9;\n }\n if(dp[i][j].count(val)){\n return dp[i][j][val];\n }\n int ans=f(i+1,j,val&v[i],v,av);\n if((val&v[i])==av[j]){\n ans=min(ans,v[i]+f(i+1,j+1,(1<<17)-1,v,av));\n\n }\n return dp[i][j][val]=ans;\n\n \n }\n int minimumValueSum(vector<int>&v, vector<int>&av) {\n n=v.size(),m=av.size();\n //keep track of whatever andsums you are able to make\n dp.resize(n+1,vector<map<int,int>>(11));\n int ans=f(0,0,(1<<17)-1,v,av);\n return (ans==1e9)?-1:ans;\n }\n};", "memory": "319957" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int solve(int ind, int grpId, int _and, vector<int>& nums, vector<int>& andValues, vector<vector<unordered_map<int, int>>>& dp, int& n, int& m) {\n if (ind >= n) {\n if (grpId >= m) return 0;\n else return 1e9;\n }\n if (grpId >= m) return 1e9;\n if (_and < andValues[grpId]) return 1e9;\n\n if (dp[ind][grpId].count(_and)) {\n return dp[ind][grpId][_and];\n }\n \n int res = 1e9;\n if ((_and & nums[ind]) == andValues[grpId]) {\n res = nums[ind] + solve(ind+1, grpId+1, (1<<17)-1, nums, andValues, dp, n, m);\n }\n res = min(res, solve(ind+1, grpId, (_and & nums[ind]), nums, andValues, dp, n, m));\n\n\n dp[ind][grpId][_and] = res;\n return dp[ind][grpId][_and];\n }\n \n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size(), m = andValues.size();\n vector<vector<unordered_map<int, int>>>dp = \n vector<vector<unordered_map<int, int>>>(n+1, vector<unordered_map<int, int>>(m+1));\n int ans = solve(0, 0, (1<<17)-1, nums, andValues, dp, n, m);\n return (ans == 1e9 ? -1 : ans);\n }\n};", "memory": "326192" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<vector<unordered_map<int,int>>> dp;\n int f(int i, int j, int currAnd, vector<int> &nums, vector<int> &andVals){\n int n = nums.size(), m = andVals.size();\n if(i == n && j == m) return 0;\n if(i == n || j == m || currAnd < andVals[j]) return 1e9;\n\n if(dp[i][j].count(currAnd)) return dp[i][j][currAnd];\n\n currAnd &= nums[i];\n int ans = f(i+1, j, currAnd, nums, andVals);\n\n if(currAnd == andVals[j])\n ans = min(ans, nums[i] + f(i+1, j+1, (1 << 17)-1, nums, andVals));\n\n return dp[i][j][currAnd] = ans;\n }\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n dp.resize(nums.size(), vector<unordered_map<int,int>>(andValues.size()));\n int ans = f(0, 0, (1 << 17)-1, nums, andValues);\n return ans >= 1e9 ? -1 : ans;\n }\n};", "memory": "332427" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\n\tpublic:\n\tvector<vector<unordered_map<int,int>>> dp;\n\tint helper(vector<int>& nums, vector<int>& andValues, int cur, int andCur, int And){\n\t\tif(cur>=nums.size()){\n\t\t\tif(andCur>=andValues.size()) return 0;\n\t\t\telse return 1e8;\n\t\t}\n\t\telse if(andCur>=andValues.size()) return 1e8;\n\n\t\tif(dp[cur][andCur].count(And)) return dp[cur][andCur][And];\n\n\t\tint inclu=1e8;\n\t\tif((And&nums[cur])==andValues[andCur]){\n\t\t\tinclu = nums[cur] + helper(nums, andValues, cur+1, andCur+1, ((1<<17)-1));\n\t\t}\n\t\tint exclu = helper(nums, andValues, cur+1, andCur, (And&nums[cur]));\n\n\t\treturn dp[cur][andCur][And]=min(inclu, exclu);\n\t}\n\tint minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n\t\tint n=nums.size(), m=andValues.size();\n\t\tdp.resize(n, vector<unordered_map<int,int>> (m));\n\n\t\tint ans = helper(nums, andValues, 0, 0, ((1<<17)-1));\n\t\tif(ans==1e8) return -1;\n\t\treturn ans;\n\t}\n};", "memory": "338662" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<vector<unordered_map<int, int>>> dp;\n int n, m;\n\n int rec(int i, int j, int val, vector<int>& nums, vector<int>& andValues) {\n if (i >= n) {\n return (j >= m) ? 0 : 1e9;\n } \n if (j >= m) {\n return 1e9;\n }\n\n if (dp[i][j].count(val)) {\n return dp[i][j][val]; // Memoization: Return already computed result\n }\n\n int end_here = 1e9, extend = 1e9;\n\n if ((val & nums[i]) == andValues[j]) {\n end_here = nums[i] + rec(i + 1, j + 1, (1 << 20) - 1, nums, andValues);\n }\n\n extend = rec(i + 1, j, val & nums[i], nums, andValues);\n\n return dp[i][j][val] = min(end_here, extend);\n }\n\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n n = nums.size();\n m = andValues.size();\n dp.resize(n, vector<unordered_map<int, int>>(m));\n int ans = rec(0, 0, (1 << 20) - 1, nums, andValues);\n return (ans == 1e9) ? -1 : ans;\n }\n};", "memory": "344897" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<vector<unordered_map<int, int>>> dp;\n int helper(vector<int>& nums, vector<int>& andValues, int cur, int andCur, int And){\n\t\tif(cur>=nums.size()){\n\t\t\tif(andCur>=andValues.size()) return 0;\n\t\t\telse return 1e8;\n\t\t}\n\t\telse if(andCur>=andValues.size()) return 1e8;\n\n\t\tif(dp[cur][andCur].count(And)) return dp[cur][andCur][And];\n\n\t\tint inclu=1e8;\n\t\tif((And&nums[cur])==andValues[andCur]){\n\t\t\tinclu = nums[cur] + helper(nums, andValues, cur+1, andCur+1, ((1<<17)-1));\n\t\t}\n\t\tint exclu = helper(nums, andValues, cur+1, andCur, (And&nums[cur]));\n\n\t\treturn dp[cur][andCur][And]=min(inclu, exclu);\n\t}\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n=nums.size(), m=andValues.size();\n\t\tdp.resize(n, vector<unordered_map<int,int>> (m));\n\n\t\tint ans = helper(nums, andValues, 0, 0, ((1<<17)-1));\n\t\tif(ans==1e8) return -1;\n\t\treturn ans;\n }\n};", "memory": "351132" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\n\tint helper(vector<int>& nums, vector<int>& andValues, int i, int j, int value){\n if(i == nums.size() && j == andValues.size()) return 0;\n else if(i == nums.size() || j == andValues.size()) return 1e8;\n else if(mem[i][j].count(value)) return mem[i][j][value];\n\t\tint take = (value & nums[i]) == andValues[j] ? \n nums[i] + helper(nums, andValues, i + 1, j + 1, -1) :\n 1e8;\n\t\tint notake = helper(nums, andValues, i + 1, j, (value & nums[i]));\n return mem[i][j][value] = min(take, notake);\n\t}\n vector<vector<unordered_map<int, int>>> mem;\npublic:\n\tint minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n\t\tint n = nums.size(), m = andValues.size();\n mem.resize(n, vector<unordered_map<int, int>>(m));\n\t\tint ans = helper(nums, andValues, 0, 0, -1);\n return ans >= 1e8 ? -1 : ans;\n\t}\n};", "memory": "357367" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int n, m;\n vector<int> nums;\n vector<int> andValues;\n vector<vector<unordered_map<int, int> > > memo;\n\n //end at current index if the and value permits else continue\n int solve (int indexNums, int indexValues, int currAnd) {\n if (indexNums >= n) {\n if (indexValues >= m) return 0;\n else return 1e9;\n }\n\n if(indexValues >= m) return 1e9; //ignore this branch\n if (memo[indexNums][indexValues].find(currAnd) != memo[indexNums][indexValues].end()) {\n return memo[indexNums][indexValues][currAnd];\n } \n int ans = 1e9;\n int x = nums[indexNums] & currAnd;\n\n if ((nums[indexNums] & currAnd) == andValues[indexValues]) {\n ans = min (ans, nums[indexNums] + solve (indexNums + 1, indexValues + 1, ((1<<17) -1)));\n }\n ans = min (ans, solve (indexNums + 1, indexValues, currAnd & nums[indexNums]));\n return memo[indexNums][indexValues][currAnd] = ans;\n }\n int minimumValueSum(vector<int>& num, vector<int>& andValue) {\n nums = num;\n andValues = andValue;\n n = nums.size(); m = andValue.size();\n memo.resize(n, vector<unordered_map<int,int> > (m));\n int ans = solve(0, 0, ((1<<17) - 1));\n\n return ans == 1e9 ? -1 : ans;\n }\n};", "memory": "382307" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n // int x[10010][15];\n // int t[400050];\n // build(int node,int tl,int tr,vector<int> &nums){\n // if(tl==tr){\n // t[node]=nums[i];\n // return;\n // }\n // int tm=(tl+tr)/2;\n // build(2*node,tl,tm,nums);\n // build(2*node+1,tm+1,tr,nums);\n // t[node]=t[2*node]&t[2*node+1];\n // }\n // int query(int node,int tl,int tr,int l,int r){\n // if(tl>r && tr<l) return INT_MAX;\n // if(tl>=l && tr<=r) return t[node];\n // int tm=(tl+tr)/2;\n // return query(2*node,tl,tm,l,r)&query(2*node+1,tm+1,tr,l,r);\n // }\n vector<vector<unordered_map<int,int>>> v;\n int dp(int i,int j,int And,int m,vector<int>& nums, vector<int>& AndValues){\n if(i==nums.size()){\n if(j==m) return 0;\n else return INT_MAX/100;\n }\n if(j==m) return INT_MAX/100;\n if(v[i][j].count(And)) return v[i][j][And];\n if((nums[i]&And)==AndValues[j]) return v[i][j][And]=min(nums[i]+dp(i+1,j+1,(1<<17)-1,m,nums,AndValues),dp(i+1,j,And&nums[i],m,nums,AndValues));\n return v[i][j][And]=dp(i+1,j,And&nums[i],m,nums,AndValues);\n }\n int minimumValueSum(vector<int>& nums, vector<int>& AndValues){\n //memset(x,-1,sizeof(x));\n int ans=-1,m=AndValues.size(),n=nums.size();\n v.resize(n,vector<unordered_map<int,int>>(m));\n return (dp(0,0,(1<<17)-1,m,nums,AndValues)==INT_MAX/100) ? -1: dp(0,0,(1<<17)-1,m,nums,AndValues);\n }\n};", "memory": "388542" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\n vector<vector<unordered_map<int,int>>>dp;\n int solve(vector<int>& nums, vector<int>& andValues, int i, int j, int curand){\n int n = nums.size(), m = andValues.size();\n if(i>=n){\n if(j>=m)return 0;\n return 1e9;\n }\n else if(j>=m)return 1e9;\n if(dp[i][j].count(curand))return dp[i][j][curand];\n int closehere = 1e9;\n if((curand & nums[i]) == andValues[j]){\n closehere = nums[i] + solve(nums,andValues,i+1,j+1,((1<<20)-1));\n }\n int notclosed = solve(nums,andValues,i+1,j,curand&nums[i]);\n\n return dp[i][j][curand] = min(closehere,notclosed);\n }\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size(), m = andValues.size();\n dp.resize(n+1,vector<unordered_map<int,int>>(m+1));\n\n int ans = solve(nums,andValues,0,0,(1<<20)-1);\n if(ans == 1e9)return -1;\n return ans;\n }\n};", "memory": "394777" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n\n vector<vector<unordered_map<int, int>>> dp;\n\n long long solve(int i, int j, vector<int>& nums, vector<int>& andValue, int value){\n int n = nums.size();\n int m = andValue.size();\n if(i >= n && j >= m) return 0;\n if(i >= n) return INT_MAX;\n if(j >= m) return INT_MAX;\n long long ans = INT_MAX;\n\n if(dp[i][j].count(value)) return dp[i][j][value];\n\n \n \n if((value & nums[i]) == andValue[j]){\n ans = (long long)(nums[i] + solve(i+1, j+1, nums, andValue, (1<<20)-1));\n }\n ans = min(ans, (long long)(solve(i+1, j, nums, andValue, value&nums[i])));\n\n return dp[i][j][value] = ans;\n }\n\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n int n = nums.size(), m = andValues.size();\n dp.resize(n+1, vector<unordered_map<int, int>>(m+1));\n int ans = solve(0, 0, nums, andValues, (1<<20)-1);\n if(ans == INT_MAX) return -1;\n return ans;\n }\n};", "memory": "401012" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<vector<unordered_map<int,int>>> dp;\n vector<int> N, A;\n int n, m;\n\n int find(int loc, int lef, int andi){\n if(loc == n && lef == m) return 0;\n else if(loc == n) return 1e9;\n else if(lef == m) return 1e9;\n\n if(dp[loc][lef][andi] != 0) return dp[loc][lef][andi];\n\n int ans = 1e9;\n\n if((andi&N[loc]) == A[lef]){\n ans = min(ans, N[loc] + find(loc+1, lef+1, (1<<21) - 1));\n }\n\n ans = min(ans, find(loc+1, lef, (andi&N[loc])));\n\n // cout<<loc<<\" \"<<lef<<\" \"<<andi<<\" \"<<ans<<\" IJIJIJ\\n\";\n\n return dp[loc][lef][andi] = ans;\n }\n\n int minimumValueSum(vector<int>& Nn, vector<int>& Aa) {\n N = Nn;\n A = Aa;\n n = N.size();\n m = A.size();\n\n dp = vector<vector<unordered_map<int,int>>> (n+1, vector<unordered_map<int,int>> (m+1));\n\n\n\n int ans = find(0,0, (1LL<<21) - 1);\n if(ans == 1e9) return -1;\n else return ans;\n\n }\n};", "memory": "401012" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
2
{ "code": "#define ll long long\n\nclass Solution {\npublic:\n vector<vector<unordered_map<ll,ll>>> dp;\n ll solve(int i,int k,ll val,vector<int>& nums,vector<int>& andVal){\n int m=andVal.size();\n int n=nums.size();\n if(k==m && i==n) return 0;\n\n if(i==n) return 1e15;\n if(k==m) return 1e15;\n val= val & nums[i];\n if(dp[i][k].find(val)!=dp[i][k].end()) return dp[i][k][val];\n ll ans=1e15;\n ll take=1e15;\n ll nott=1e15;\n if(val==andVal[k]){\n take=nums[i]+solve(i+1,k+1,(1<<17)-1,nums,andVal);\n // ans=min(ans,nums[i]+solve(i+1,k+1,nums,andVal));\n }\n nott=solve(i+1,k,val,nums,andVal);\n // val=val&nums[j];\n // if(val==andVal[k]){\n // ans=min(ans,nums[j]+solve(j+1,k+1,nums,andVal));\n // }\n // for(int j=i+1;j<n;j++){\n // }\n\n return dp[i][k][val]=min(nott,take);\n \n }\n\n int minimumValueSum(vector<int>& nums, vector<int>& andVal) {\n int m=andVal.size();\n int n=nums.size();\n dp.resize(n+1,vector<unordered_map<ll,ll>>(m+1));\n ll ans=solve(0,0,(1<<17)-1,nums,andVal);\n return ans== 1e15 ? -1 : ans;\n }\n};", "memory": "407247" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
2
{ "code": "vector<vector<unordered_map<int,int>>>dp;\nconst int changer = 20;\n#define outo(x) cout << x << \" \"\n#define out(x, y, z) cout << x << \" \" << y << \" \" << z << endl\n\nclass Solution {\npublic:\n int inf = 10000007;\n int findmini(int curval,int idx1,vector<int>&a, int idx2, vector<int>&v)\n {\n int n = a.size();\n int m = v.size();\n if(idx1==n)\n {\n if(idx2 == m)\n return 0;\n return inf;\n }\n\n else if(idx2 == m)\n {\n return inf;\n }\n int ans1 = inf, ans2 = inf;\n \n if(dp[idx1][idx2][curval])\n return dp[idx1][idx2][curval];\n\n if((curval&a[idx1])==v[idx2])\n {\n ans1 = a[idx1] + findmini((1<<changer)-1,idx1+1,a,idx2+1,v);\n }\n \n ans2=findmini(curval&a[idx1],idx1+1,a,idx2,v);\n \n dp[idx1][idx2][curval] = min(ans1,ans2);\n return dp[idx1][idx2][curval];\n }\n int minimumValueSum(vector<int>& a, vector<int>& v) {\n int n = a.size();\n dp.clear();\n dp.resize(n,vector<unordered_map<int,int>>(10));\n int ans = findmini((1<<changer)-1,0,a,0,v);\n if(ans == inf)\n ans = -1;\n return ans;\n }\n};", "memory": "413482" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int n;\n int m;\n vector<vector<unordered_map<int, int>>> dp;\n //unordered_map<int, unordered_map<int, unordered_map<int,int>>> dp;\n int solve(int i,int j, vector<int>& nums, vector<int> & andValues, int ansop){\n if(i>=n){\n if(j>=m){\n return 0;\n }\n else{\n return 1e9;\n }\n }\n else if(j>=m){\n return 1e9;\n }\n if(dp[i][j].count(ansop)) {\n return dp[i][j][ansop];\n }\n int take=1e9;\n if((ansop & nums[i])==andValues[j]){\n take=nums[i]+solve(i+1,j+1,nums,andValues,-1);\n }\n int nottake=solve(i+1,j,nums,andValues,(ansop & nums[i]));\n return dp[i][j][ansop]=min(nottake,take);\n }\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n n=nums.size();\n m=andValues.size();\n dp.resize(n, vector<unordered_map<int, int>>(11));\n int ansop=-1;\n if(solve(0,0,nums,andValues,ansop)==1e9){\n return -1;\n }\n return solve(0,0,nums,andValues,ansop);\n }\n};", "memory": "419717" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int n;\n int m;\n\n vector<vector<unordered_map<int,int>>> dp;\n int solve(vector<int>& nums, vector<int>& andValues,int i,int j,int andOp){\n // base case\n if(i>=n){\n if(j>=m) return 0;\n else return 1e9;\n } else if(j>=m) return 1e9;\n\n // recursive call\n if(dp[i][j].count(andOp)){\n return dp[i][j][andOp];\n }\n int take_i=1e9;\n\n if((andOp & nums[i])==andValues[j]){\n take_i=nums[i]+solve(nums,andValues,i+1,j+1,((1<<17)-1));\n }\n int not_take_i=solve(nums,andValues,i+1,j,andOp & nums[i]);\n \n return dp[i][j][andOp]=min(take_i,not_take_i);\n }\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n n=nums.size();\n m=andValues.size();\n\n dp.resize(n+1,vector<unordered_map<int,int>>(11));\n int result=solve(nums,andValues,0,0,((1<<17)-1));\n\n return result==1e9 ?-1 : result;\n }\n};", "memory": "419717" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\nconst int q=(1<<20)-1;\n int INF = 2e9;\n int func(int n, int m, int i, int j, vector<vector<unordered_map<int, int>>>& dp, int range, vector<int>& nums, vector<int>& andValues) {\n if(i==n) {\n if(j==m && range==q) return 0;\n return INF;\n }\n if (dp[i][j].find(range) != dp[i][j].end()) return dp[i][j][range];\n int take = INF;\n if (i < n && j < m && andValues[j] == (range&nums[i])) {\n take = nums[i] + func(n, m, i + 1, j + 1, dp, q, nums, andValues);\n }\n int take_pull = func(n, m, i + 1, j, dp, nums[i] & range, nums, andValues);\n\n return dp[i][j][range] = min(take, take_pull);\n }\n\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n // simpul dp, 2 segment trees -> for AND and min , 0(n*logn*logn)[mostly cant implement/TLE]\n // much better, vector<vector<map>> dp(n , vector<map>(m)))\n // BECAUSE AT MOST 30 DIFFERENT VALUES OF \"andVal\"**\n \n int n = nums.size();\n int m = andValues.size();\n int range = (1 << 20) - 1; \n vector<vector<unordered_map<int, int>>> dp(n, vector<unordered_map<int, int>>(m+1));\n int ans = func(n, m, 0, 0, dp, range, nums, andValues);\n if(ans==INF) return -1;\n return ans;\n }\n};", "memory": "425952" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\nconst int q=(1<<20)-1;\n int INF = 2e9;\n int func(int n, int m, int i, int j, vector<vector<unordered_map<int, int>>>& dp, int range, vector<int>& nums, vector<int>& andValues) {\n if(i==n) {\n if(j==m && range==q) return 0;\n return INF;\n }\n if (dp[i][j].find(range) != dp[i][j].end()) return dp[i][j][range];\n int take = INF;\n if (i < n && j < m && andValues[j] == (range&nums[i])) {\n take = nums[i] + func(n, m, i + 1, j + 1, dp, q, nums, andValues);\n }\n int take_pull = func(n, m, i + 1, j, dp, nums[i] & range, nums, andValues);\n\n return dp[i][j][range] = min(take, take_pull);\n }\n\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n // simpul dp, 2 segment trees -> for AND and min , 0(n*logn*logn)[mostly cant implement/TLE]\n // much better, vector<vector<map>> dp(n , vector<map>(m)))\n // BECAUSE AT MOST 30 DIFFERENT VALUES OF \"andVal\"**\n \n int n = nums.size();\n int m = andValues.size();\n int range = (1 << 20) - 1; \n vector<vector<unordered_map<int, int>>> dp(n, vector<unordered_map<int, int>>(m+1));\n int ans = func(n, m, 0, 0, dp, range, nums, andValues);\n if(ans==INF) return -1;\n return ans;\n }\n};", "memory": "432187" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n vector<vector<unordered_map<int,int> > > dp;\n int solve(int i, int j, int mask, vector<int> &nums, vector<int> &v)\n {\n if(j==v.size())\n {\n return (i>=nums.size()) ? 0 : 1e9;\n }\n\n if(dp[i][j].count(mask)) return dp[i][j][mask];\n\n if(i>=nums.size())\n {\n return 1e9;\n }\n\n int ans = 1e9;\n if((nums[i]&mask) == v[j])\n {\n ans = min(ans,nums[i] + solve(i+1,j+1,(1<<20)-1,nums,v));\n }\n ans = min(ans,solve(i+1,j,(nums[i]&mask),nums,v));\n\n return dp[i][j][mask] = ans;\n }\n\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n dp.resize(nums.size()+5, vector<unordered_map<int,int> > (12));\n int ans = solve(0,0,(1<<20)-1,nums,andValues);\n return ans == 1e9 ? -1 : ans; \n }\n};", "memory": "432187" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int solve(int i,int j,vector<int> &nums,vector<int> &an,unordered_map<int,unordered_map<int,unordered_map<int,int> > > &dp,int x)\n {\n if(j>=an.size())\n return 1e6;\n if(i>=nums.size())\n {\n if(x==an.back() && j+1==an.size())\n return nums[i-1];\n return 1e6;\n }\n \n if(dp.count(i) && dp[i].count(j) && dp[i][j].count(x))\n return dp[i][j][x];\n int t=1e6,nt=1e6;\n if((x)==an[j])\n t=nums[i-1]+solve(i+1,j+1,nums,an,dp,nums[i]);\n nt=solve(i+1,j,nums,an,dp,x & nums[i]);\n return dp[i][j][x]=min(t,nt);\n }\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n unordered_map<int,unordered_map<int,unordered_map<int,int> > > dp;\n int x=solve(1,0,nums,andValues,dp,nums[0]);\n if(x>=1e6)\n return -1;\n return x;\n }\n};", "memory": "438422" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> a, b;\n int n, m;\n unordered_map<int,unordered_map<int,unordered_map<int,int>>> dp;\n int inf = 1000000;\n int ans(int i, int j, int k) {\n if (i == n and j == m)return 0;\n if (i == n or j == m)return inf;\n int p=inf;\n if(dp.find(i)!=dp.end() and dp[i].find(j)!=dp[i].end() and\n dp[i][j].find(k)!=dp[i][j].end())return dp[i][j][k];\n if (k == b[j])p = min(p, a[i]+ans(i+1,j+1,a[i+1]));\n return dp[i][j][k] = min(p,ans(i+1,j,k&a[i+1]));\n }\n int minimumValueSum(vector<int>& aa, vector<int>& bb) {\n a = aa, b = bb;\n a.push_back(0), b.push_back(0);\n n = aa.size(), m = bb.size();\n int p = ans(0, 0, a[0]);\n if (p >= inf)p = -1;\n return p;\n };\n};", "memory": "438422" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\n int n,m;\n vector<vector<unordered_map<int,int>>>dp;\n\n int f(int i, int j, int curr, vector<int>&nums, vector<int>&v)\n {\n if(i == n)\n {\n if(j < m-1) return 1e9;\n if(j == m-1)\n {\n if(curr == v[m-1]) return nums.back();\n return 1e9;\n }\n if(curr == (1<<20)-1) return 0;\n return 1e9;\n }\n if(j == m) return 1e9;\n\n if(dp[i][j].find(curr) != dp[i][j].end()) return dp[i][j][curr];\n\n int not_take = f(i+1, j, curr & nums[i], nums, v);\n int take = 1e9;\n if((curr & nums[i]) == v[j])\n {\n take = nums[i] + f(i+1, j+1, (1<<20)-1, nums, v);\n }\n return dp[i][j][curr] = min(take, not_take);\n }\n\npublic:\n int minimumValueSum(vector<int>& nums, vector<int>& v) {\n n = nums.size();\n m = v.size();\n dp.resize(n,vector<unordered_map<int,int>>(m));\n int res = f(0,0,(1<<20)-1,nums,v);\n if(res == 1e9) return -1;\n return res; \n }\n};", "memory": "444657" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int solve(vector<int>&nums,vector<int>& andValues,int curr,int idx,int currAnd, vector<vector<unordered_map<int,int>>>&dp)\n {\n if(curr==nums.size())\n {\n if(idx==andValues.size())\n return 0;\n return 1e8;\n }\n if(idx==andValues.size())\n return 1e8;\n if(dp[curr][idx].find(currAnd)!=dp[curr][idx].end())\n return dp[curr][idx][currAnd];\n int notTake=solve(nums,andValues,curr+1,idx,currAnd&nums[curr],dp);\n if((currAnd&nums[curr])==andValues[idx])\n {\n int take=solve(nums,andValues,curr+1,idx+1,(1<<17)-1,dp);\n if(take!=1e8)\n take+=nums[curr];\n notTake=min(notTake,take);\n }\n return dp[curr][idx][currAnd]=notTake;\n }\n int minimumValueSum(vector<int>& nums, vector<int>& andValues)\n {\n int n=nums.size();\n int m=andValues.size();\n vector<vector<unordered_map<int,int>>> dp(n,vector<unordered_map<int,int>>(m));\n int val=solve(nums,andValues,0,0,(1<<17)-1,dp);\n if(val==1e8)\n return -1;\n return val;\n }\n};", "memory": "444657" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n#define ll long long\nint n,m;\n// ll dp[10001][11];\nvector<vector<unordered_map<int,int>>> dp;\nint f(int i,int j,int curand,vector<int>& nums, vector<int>& andv){\n \n if(i==n){\n if(j<m-1) return 1e9;\n if(j==m-1){\n if(curand == andv[m-1]) return nums[n-1];\n return 1e9;\n }\n if( curand==((1<<17)-1)) return 0;\n return 1e9;\n }\n if(j>=m) return 1e9;\n if(dp[i][j].find(curand)!=dp[i][j].end()){\n return dp[i][j][curand];\n }\n // if(dp[i][j]!=-1){\n // return dp[i][j];\n // }\n int inc=f(i+1,j,(curand & nums[i]),nums,andv);\n int exc=1e9;\n if((curand & nums[i] )== andv[j]){\n exc=nums[i]+f(i+1,j+1,((1<<17)-1),nums,andv);\n }\n // return min(inc,exc);\n return dp[i][j][curand] =min(inc,exc);\n}\n\n int minimumValueSum(vector<int>& nums, vector<int>& andv) {\n n=nums.size(),m=andv.size();\n // memset(dp,-1,sizeof(dp));\n dp.resize(n+1,vector<unordered_map<int,int>>(m+1));\n int ans=f(0,0,((1<<17)-1),nums,andv);\n if(ans>=1e9){\n return -1;\n }\n return ans;\n }\n};", "memory": "450892" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\nint n;\nint m;\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n if(nums[0]<andValues[0])return -1;\n n=nums.size();\n m=andValues.size();\n vector<vector<unordered_map<int, int>>> dp(n+1, vector<unordered_map<int, int>>(11));\n int ans = solve(dp, nums, andValues, 0, 0, (1<<17)-1);\n return (ans>=1e7)?-1:ans;\n }\n\n int solve(vector<vector<unordered_map<int, int>>>& dp, vector<int>& nums, vector<int>& andValues, int index, int partition, int curr){\n if(index>=n){\n if(partition>=m)return 0;\n return 1e7;\n }\n if(partition>=m)return 1e7;\n if(dp[index][partition].count(curr))return dp[index][partition][curr];\n int ans=1e7;\n if(curr>=andValues[partition] && nums[index]>=andValues[partition]){\n ans = min(ans, solve(dp, nums, andValues, index+1, partition, curr&nums[index]));\n if((curr&nums[index])==andValues[partition])ans = min(ans, nums[index]+solve(dp, nums, andValues, index+1, partition+1, (1<<17)-1));\n }\n return dp[index][partition][curr]=ans;\n }\n};", "memory": "450892" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
3
{ "code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\nusing ll=long long;\ntypedef tree <pair<ll,ll>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n/*\n order_of_key (k)\n find_by_order(k)\n*/\ntemplate<typename T> istream & operator>>(istream & in, vector<T> & lst)\n{ for (auto & e : lst) in >> e; return in; }\n#define endl '\\n'\n#define F first\n#define pb push_back\n#define pf push_front\n#define pob pop_back\n#define pof pop_front\n#define S second\n#define MP make_pair\n// typedef long long ll;\ntypedef long double ld;\ntypedef pair<ll, ll> ii;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<ii> vii;\ntypedef set<ll> si;\ntypedef map<string, ll> msi;\n#define all(v) v.begin(),v.end()\n#define REP(i, a, b) \\\nfor (ll i = ll(a); i <= ll(b); i++)\n#define RREP(i, a, b) \\\nfor (ll i = ll(a); i >= ll(b); i--)\n#define ohyes cout<<\"YES\\n\"\n#define ohno cout<<\"NO\\n\"\n#define getvec(v,a,b) \\\nfor (ll i = a; i <=b; i++) \\\n cin >> v[i];\n#define printvec(v,a,b) {for (ll i = a; i <=b; i++){cout << v[i] << \" \";}cout<<'\\n';}\n#define get2dvec(v,a,b,c,d) \\\nfor (ll i = a; i <=b; i++) \\\n for (ll j = c; j <=d; j++) \\\n cin >> v[i][j];\n#define print2dvec(v,a,b,c,d) for (ll i = a; i <=b; i++){for (ll j = c; j <=d; j++){cout << v[i][j]<<\" \";}cout<<'\\n';}\n#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n// ll mod = 998244353;\n// ll mod = 1e9 + 7;\nll _t,cs;\n// cout<<\"Case \"<<cs-_t<<\": \"<<ans<<'\\n';\n// memset(dp,-1,sizeof(dp));\nunordered_map<ll,unordered_map<ll,unordered_map<ll,ll>>> dp;\nll pref[33][10010];\nclass Solution {\npublic:\n ll n,m; \n vector<int> vn,vm;\n ll findand(ll l,ll r){\n ll ans=0;\n REP(i,0,32){\n ll cnt=pref[i][r+1]-pref[i][l];\n if(cnt==r-l+1)ans|=(1LL<<i);\n }\n return ans;\n }\n ll rec(ll lev,ll mind,ll msk){ \n if(lev==n){\n if(mind==m && msk==vm[m-1])return 0;\n if(mind==m-1 && msk==vm[m-1])return vn[n-1];\n return 1e11;\n }\n if(mind>=m)return 1e11;\n if(dp.find(lev)!=dp.end() && dp[lev].find(mind)!=dp[lev].end() && dp[lev][mind].find(msk)!=dp[lev][mind].end())return dp[lev][mind][msk];\n auto &ans=dp[lev][mind][msk];\n // if(ans!=-1)return ans;\n ans=1e11;\n // cout<<lev<<\" \"<<mind<<\" \"<<msk<<'\\n';\n if(msk==vm[mind])ans=min(ans,vn[lev-1]+rec(lev+1,mind+1,vn[lev]));\n if(msk>=vm[mind])ans=min(ans,rec(lev+1,mind,msk&vn[lev]));\n return ans;\n }\n int minimumValueSum(vector<int>& vnn, vector<int>& vmm) {\n // if(vnn.back()==65535 && vmm.size()==2)return -1;\n vn=vnn;\n vm=vmm;\n n=vn.size();\n m=vm.size();\n REP(i,0,32){\n pref[i][0]=0;\n REP(j,1,n){\n pref[i][j]=pref[i][j-1];\n if((1LL<<i)&vn[j-1])pref[i][j]++;\n // if(i<12)dp[j-1][i]=-1;\n // if(i<12)dp[j][i]=-1;\n }\n }\n dp.clear();\n ll ans=rec(0,0,(1LL<<20)-1);\n // REP(i,0,n-1){\n // if(findand(0,i)==vm[0])ans=min(ans,vn[i]+rec(i+1,1));\n // }\n\n if(ans<1e11)return ans;\n return -1;\n }\n};", "memory": "457127" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
3
{ "code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\nusing ll=long long;\ntypedef tree <pair<ll,ll>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n/*\n order_of_key (k)\n find_by_order(k)\n*/\ntemplate<typename T> istream & operator>>(istream & in, vector<T> & lst)\n{ for (auto & e : lst) in >> e; return in; }\n#define endl '\\n'\n#define F first\n#define pb push_back\n#define pf push_front\n#define pob pop_back\n#define pof pop_front\n#define S second\n#define MP make_pair\n// typedef long long ll;\ntypedef long double ld;\ntypedef pair<ll, ll> ii;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<ii> vii;\ntypedef set<ll> si;\ntypedef map<string, ll> msi;\n#define all(v) v.begin(),v.end()\n#define REP(i, a, b) \\\nfor (ll i = ll(a); i <= ll(b); i++)\n#define RREP(i, a, b) \\\nfor (ll i = ll(a); i >= ll(b); i--)\n#define ohyes cout<<\"YES\\n\"\n#define ohno cout<<\"NO\\n\"\n#define getvec(v,a,b) \\\nfor (ll i = a; i <=b; i++) \\\n cin >> v[i];\n#define printvec(v,a,b) {for (ll i = a; i <=b; i++){cout << v[i] << \" \";}cout<<'\\n';}\n#define get2dvec(v,a,b,c,d) \\\nfor (ll i = a; i <=b; i++) \\\n for (ll j = c; j <=d; j++) \\\n cin >> v[i][j];\n#define print2dvec(v,a,b,c,d) for (ll i = a; i <=b; i++){for (ll j = c; j <=d; j++){cout << v[i][j]<<\" \";}cout<<'\\n';}\n#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n// ll mod = 998244353;\n// ll mod = 1e9 + 7;\nll _t,cs;\n// cout<<\"Case \"<<cs-_t<<\": \"<<ans<<'\\n';\n// memset(dp,-1,sizeof(dp));\nunordered_map<ll,unordered_map<ll,unordered_map<ll,ll>>> dp;\nll pref[33][10010];\nclass Solution {\npublic:\n ll n,m; \n vector<int> vn,vm;\n ll findand(ll l,ll r){\n ll ans=0;\n REP(i,0,32){\n ll cnt=pref[i][r+1]-pref[i][l];\n if(cnt==r-l+1)ans|=(1LL<<i);\n }\n return ans;\n }\n ll rec(ll lev,ll mind,ll msk){ \n if(lev==n){\n if(mind==m && msk==vm[m-1])return 0;\n if(mind==m-1 && msk==vm[m-1])return vn[n-1];\n return 1e11;\n }\n if(mind>=m)return 1e11;\n if(dp.find(lev)!=dp.end() && dp[lev].find(mind)!=dp[lev].end() && dp[lev][mind].find(msk)!=dp[lev][mind].end())return dp[lev][mind][msk];\n auto &ans=dp[lev][mind][msk];\n // if(ans!=-1)return ans;\n ans=1e11;\n // cout<<lev<<\" \"<<mind<<\" \"<<msk<<'\\n';\n if(msk==vm[mind])ans=min(ans,vn[lev-1]+rec(lev+1,mind+1,vn[lev]));\n if(msk>=vm[mind])ans=min(ans,rec(lev+1,mind,msk&vn[lev]));\n return ans;\n }\n int minimumValueSum(vector<int>& vnn, vector<int>& vmm) {\n // if(vnn.back()==65535 && vmm.size()==2)return -1;\n vn=vnn;\n vm=vmm;\n n=vn.size();\n m=vm.size();\n REP(i,0,32){\n pref[i][0]=0;\n REP(j,1,n){\n pref[i][j]=pref[i][j-1];\n if((1LL<<i)&vn[j-1])pref[i][j]++;\n // if(i<12)dp[j-1][i]=-1;\n // if(i<12)dp[j][i]=-1;\n }\n }\n dp.clear();\n ll ans=rec(0,0,(1LL<<20)-1);\n // REP(i,0,n-1){\n // if(findand(0,i)==vm[0])ans=min(ans,vn[i]+rec(i+1,1));\n // }\n\n if(ans<1e11)return ans;\n return -1;\n }\n};", "memory": "463362" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int n;\n int m;\n //vector<vector<unordered_map<int, int>>> dp;\n unordered_map<int, unordered_map<int, unordered_map<int,int>>> dp;\n int solve(int i,int j, vector<int>& nums, vector<int> & andValues, int ansop){\n if(i>=n){\n if(j>=m){\n return 0;\n }\n else{\n return 1e9;\n }\n }\n else if(j>=m){\n return 1e9;\n }\n if(dp[i][j].count(ansop)) {\n return dp[i][j][ansop];\n }\n int take=1e9;\n if((ansop & nums[i])==andValues[j]){\n take=nums[i]+solve(i+1,j+1,nums,andValues,-1);\n }\n int nottake=solve(i+1,j,nums,andValues,(ansop & nums[i]));\n return dp[i][j][ansop]=min(nottake,take);\n }\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n n=nums.size();\n m=andValues.size();\n //dp.resize(n, vector<unordered_map<int, int>>(11));\n int ansop=-1;\n if(solve(0,0,nums,andValues,ansop)==1e9){\n return -1;\n }\n return solve(0,0,nums,andValues,ansop);\n }\n};", "memory": "463362" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\n /*\n Recursion with memorization\n */\npublic:\n int MAX = 131071;\n vector<vector<unordered_map<int, int>>> memo;\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n memo = vector<vector<unordered_map<int, int>>>(nums.size()+1, vector<unordered_map<int, int>>(andValues.size()+1));\n\n return check(nums, andValues, 0, 0, MAX);\n }\n\n int check(vector<int>& nums, vector<int>& andValues, int i, int j, int mask) {\n if (memo[i][j].count(mask) != 0) {\n return memo[i][j][mask];\n }\n int res;\n //printf(\"A i=%d,j=%d,mask=%d\\n\", i, j, mask);\n if (i == nums.size() && j == andValues.size()) {\n res = 0;\n } else if (i == nums.size() || j == andValues.size()) {\n res = -1;;\n } else {\n mask &= nums[i];\n //printf(\"B i=%d,j=%d,mask=%d\\n\", i, j, mask);\n if (mask < andValues[j]) {\n // If we have \"anded\" too many values\n res = -1;\n } else if (mask == andValues[j]) {\n int sum_1 = check(nums, andValues, i+1, j, mask);\n int sum_2 = check(nums, andValues, i+1, j+1, MAX);\n //printf(\"i=%d,j=%d,sum1=%d,sum2=%d\\n\", i, j, sum_1, sum_2);\n if (sum_1 == -1 && sum_2 == -1) {\n res = -1;\n } else if (sum_1 == -1) {\n res = nums[i] + sum_2;\n } else if (sum_2 == -1) {\n res = sum_1;\n } else {\n res = min(sum_1, nums[i] + sum_2);\n }\n } else {\n res = check(nums, andValues, i+1, j, mask);\n }\n }\n memo[i][j][mask] = res;\n return res;\n }\n};", "memory": "469597" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n// int solve(vector<vector<int>> &pre , int l ,int k , vector<int> &And, vector<int> &nums)\n// {\n\n \n \n// if(l == nums.size() &&k == And.size()) return 0;\n// if(l == nums.size() || k == And.size()) return 1e9;\n// int maxm = 1e9;\n// // cout<<\"yes\"<<endl;\n// for(int i = l ; i<nums.size() ; i++)\n// {\n// // cout<<\"yes\"<<endl;\n// int Andval = 0;\n// for(int j = 0 ; j<32 ; j++)\n// {\n// // cout<<pre[l-1][j]<<\" \"<<pre[i][j]<<endl;\n// if(pre[l][j] - pre[i+1][j] == 0)\n// {\n// Andval = Andval|(1<<j);\n// }\n// }\n// // cout<<Andval<<endl;\n// if(Andval == And[k])\n// {\n// // cout<<\"yes2\"<<endl;\n// maxm = min(maxm , nums[i]+solve(pre , i+1 , k+1 , And , nums ));\n// // cout<<maxm<<\"maxm\"<<endl;\n// }\n// }\n// return maxm;\n// }\n// unordered_map<long long , int> mym;\nvector<vector<unordered_map<int , int>>> mym;\nint solve(vector<int> &nums , int i , int AND , int k , vector<int>&Andv)\n{\n // cout<<AND<<endl;\n // long long key = (((long long)i<<30) || ((long long)k<<19) || ((long long)AND));\n // if(mym.find(key) !=mym.end()) return mym[key];\n \n if(i == nums.size() && k == Andv.size()) {return 0;}\n if(i==nums.size() || k == Andv.size()) return 1e9;\n if(mym[i][k].find(AND) != mym[i][k].end()) return mym[i][k][AND]; \n int newandval = AND&nums[i];\n int notpick = solve(nums , i+1 ,newandval , k , Andv);\n int pick = 1e9;\n // cout<<newandval<<endl;\n if(newandval == Andv[k])\n {\n // cout<<AND<<endl;\n \n pick = nums[i] + solve(nums , i+1 ,(1<<18)-1 ,k+1 , Andv);\n }\n return mym[i][k][AND] = min(pick , notpick);\n}\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n vector<vector<int>> temp = vector<vector<int>>(nums.size()+1 , vector<int>(32 , 0));\n // for(int i = 0 ; i<nums.size() ; i++)\n // {\n // int x = nums[i];\n // for(int j = 0 ; j<32 ; j++)\n // {\n // temp[i+1][j] = temp[i][j];\n // if((nums[i]&(1<<j)) == 0) temp[i+1][j]++; \n // }\n // }\n // for(auto & x : temp)\n // {\n // for(int i = 0 ; i<32 ; i++)\n // {\n // cout<<x[i]<<\" \";\n // }\n // cout<<endl;\n // }\n mym = vector<vector<unordered_map<int , int>>>(nums.size()+1 , vector<unordered_map<int , int>>(andValues.size()+1));\n int ans = solve(nums , 0 ,(1<<18)-1,0, andValues );\n if(ans>=1e9) return -1;\n return ans;\n // return 0;\n }\n};", "memory": "469597" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n#define ll long long int\nunordered_map<int,unordered_map<int,unordered_map<int,int>>>mp;\nll func(int ind,int ind1,int n,int m,vector<int>&nums,vector<int>&a,int res){\n // cout<<ind<<\" \"<<ind1<<endl;\n if(ind>=n && ind1>=m)\n return 0;\n if(ind>=n || ind1>=m)\n return INT_MAX;\n if(mp[ind][ind1].find(res)!=mp[ind][ind1].end())\n return mp[ind][ind1][res];\n ll ans=INT_MAX;\n ans=min(ans,func(ind+1,ind1,n,m,nums,a,(res&nums[ind])));\n if((res&nums[ind])==a[ind1])\n {\n // cout<<res<<\" \"<<ind<<\" \"<<ind1<<endl;\n ans=min(ans,func(ind+1,ind1+1,n,m,nums,a,INT_MAX)+nums[ind]);\n }\n return mp[ind][ind1][res]=ans;\n}\n int minimumValueSum(vector<int>& nums, vector<int>& a) {\n int n=nums.size(),m=a.size();\n ll ans= func(0,0,n,m,nums,a,INT_MAX);\n if(ans>=INT_MAX)\n return -1;\n return ans;\n }\n};", "memory": "475832" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long pos(vector<int>&nums,vector<int>&andvalues,int ind,int value,int covered,unordered_map<int,unordered_map<int,unordered_map<int,int>>>&dp){\n if(covered>=andvalues.size())return 1e6;\n if(ind>=nums.size()){\n if(value==andvalues[andvalues.size()-1] && covered+1==andvalues.size())return nums[ind-1];\n return 1e6;\n }\n if(dp.find(ind)!=dp.end() && dp[ind].find(covered)!=dp[ind].end() && dp[ind][covered].find(value)!=dp[ind][covered].end())return dp[ind][covered][value];\n long long select=1e6,unselect=1e6;\n if(value==andvalues[covered]){\n unselect=pos(nums,andvalues,ind+1,value&nums[ind],covered,dp);\n select=nums[ind-1]+pos(nums,andvalues,ind+1,nums[ind],covered+1,dp);\n return dp[ind][covered][value]=min(unselect,select);\n }\n unselect=pos(nums,andvalues,ind+1,value&nums[ind],covered,dp);\n return dp[ind][covered][value]=min(select,unselect);\n }\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n // vector<vector<vector<int>>>dp(nums.size()+1,vector<vector<int>>(andValues.size()+1,vector<int>(1e5,-1)));\n unordered_map<int,unordered_map<int,unordered_map<int,int>>>dp;\n long long res=pos(nums,andValues,1,nums[0],0,dp);\n return (res>=1e6)?-1:res;\n }\n};", "memory": "482067" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long pos(vector<int>&nums,vector<int>&andvalues,int ind,int value,int covered,unordered_map<int,unordered_map<int,unordered_map<int,int>>>&dp){\n if(covered>=andvalues.size())return 1e6;\n if(ind>=nums.size()){\n if(value==andvalues[andvalues.size()-1] && covered+1==andvalues.size())return nums[ind-1];\n return 1e6;\n }\n if(dp.find(ind)!=dp.end() && dp[ind].find(covered)!=dp[ind].end() && dp[ind][covered].find(value)!=dp[ind][covered].end())return dp[ind][covered][value];\n long long select=1e6,unselect=1e6;\n if(value==andvalues[covered]){\n unselect=pos(nums,andvalues,ind+1,value&nums[ind],covered,dp);\n select=nums[ind-1]+pos(nums,andvalues,ind+1,nums[ind],covered+1,dp);\n return dp[ind][covered][value]=min(unselect,select);\n }\n unselect=pos(nums,andvalues,ind+1,value&nums[ind],covered,dp);\n return dp[ind][covered][value]=min(select,unselect);\n }\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n // vector<vector<vector<int>>>dp(nums.size()+1,vector<vector<int>>(andValues.size()+1,vector<int>(1e5,-1)));\n unordered_map<int,unordered_map<int,unordered_map<int,int>>>dp;\n long long res=pos(nums,andValues,1,nums[0],0,dp);\n return (res>=1e6)?-1:res;\n }\n};", "memory": "488302" }
3,364
<p>You are given two arrays <code>nums</code> and <code>andValues</code> of length <code>n</code> and <code>m</code> respectively.</p> <p>The <strong>value</strong> of an array is equal to the <strong>last</strong> element of that array.</p> <p>You have to divide <code>nums</code> into <code>m</code> <strong>disjoint contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> such that for the <code>i<sup>th</sup></code> subarray <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, the bitwise <code>AND</code> of the subarray elements is equal to <code>andValues[i]</code>, in other words, <code>nums[l<sub>i</sub>] &amp; nums[l<sub>i</sub> + 1] &amp; ... &amp; nums[r<sub>i</sub>] == andValues[i]</code> for all <code>1 &lt;= i &lt;= m</code>, where <code>&amp;</code> represents the bitwise <code>AND</code> operator.</p> <p>Return <em>the <strong>minimum</strong> possible sum of the <strong>values</strong> of the </em><code>m</code><em> subarrays </em><code>nums</code><em> is divided into</em>. <em>If it is not possible to divide </em><code>nums</code><em> into </em><code>m</code><em> subarrays satisfying these conditions, return</em> <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3,3,2], andValues = [0,3,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The only possible way to divide <code>nums</code> is:</p> <ol> <li><code>[1,4]</code> as <code>1 &amp; 4 == 0</code>.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[3]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> <li><code>[2]</code> as the bitwise <code>AND</code> of a single element subarray is that element itself.</li> </ol> <p>The sum of the values for these subarrays is <code>4 + 3 + 3 + 2 = 12</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,5,7,7,7,5], andValues = [0,7,5]</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>There are three ways to divide <code>nums</code>:</p> <ol> <li><code>[[2,3,5],[7,7,7],[5]]</code> with the sum of the values <code>5 + 7 + 5 == 17</code>.</li> <li><code>[[2,3,5,7],[7,7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> <li><code>[[2,3,5,7,7],[7],[5]]</code> with the sum of the values <code>7 + 7 + 5 == 19</code>.</li> </ol> <p>The minimum possible sum of the values is <code>17</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4], andValues = [2]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>The bitwise <code>AND</code> of the entire array <code>nums</code> is <code>0</code>. As there is no possible way to divide <code>nums</code> into a single subarray to have the bitwise <code>AND</code> of elements <code>2</code>, return <code>-1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == andValues.length &lt;= min(n, 10)</code></li> <li><code>1 &lt;= nums[i] &lt; 10<sup>5</sup></code></li> <li><code>0 &lt;= andValues[j] &lt; 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long pos(vector<int>&nums,vector<int>&andvalues,int ind,int value,int covered,unordered_map<int,unordered_map<int,unordered_map<int,int>>>&dp){\n if(covered>=andvalues.size())return 1e6;\n if(ind>=nums.size()){\n if(value==andvalues[andvalues.size()-1] && covered+1==andvalues.size())return nums[ind-1];\n return 1e6;\n }\n if(dp.find(ind)!=dp.end() && dp[ind].find(covered)!=dp[ind].end() && dp[ind][covered].find(value)!=dp[ind][covered].end())return dp[ind][covered][value];\n long long select=1e6,unselect=1e6;\n if(value==andvalues[covered]){\n unselect=pos(nums,andvalues,ind+1,value&nums[ind],covered,dp);\n select=nums[ind-1]+pos(nums,andvalues,ind+1,nums[ind],covered+1,dp);\n return dp[ind][covered][value]=min(unselect,select);\n }\n unselect=pos(nums,andvalues,ind+1,value&nums[ind],covered,dp);\n return dp[ind][covered][value]=min(select,unselect);\n }\n int minimumValueSum(vector<int>& nums, vector<int>& andValues) {\n // vector<vector<vector<int>>>dp(nums.size()+1,vector<vector<int>>(andValues.size()+1,vector<int>(1e5,-1)));\n unordered_map<int,unordered_map<int,unordered_map<int,int>>>dp;\n long long res=pos(nums,andValues,1,nums[0],0,dp);\n return (res>=1e6)?-1:res;\n }\n};", "memory": "494537" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n int n = word.length();\n int count = 0;\n for(int i = 0;i < 26;i++){\n char c = char(int('a') + i);\n char cu = char(int (c) - 32);\n bool isl = false,isu = false;\n for(int j = 0;j < n;j++){\n if(word[j] == c) isl = true;\n else if(word[j] == cu) isu = true;\n }\n if(isu && isl) count++;\n }\n return count;\n }\n};", "memory": "8400" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n int l = 0, u = 0;\n for (char ch: word) {\n if (islower(ch)) {\n int bit = 1 << (ch-'a');\n l = l | bit;\n }\n else {\n int bit = 1 << (ch-'A');\n u = u | bit;\n }\n }\n int c = 0;\n for (int i = 0; i < 26; i++) {\n if ((l & (1<<i)) & (u & (1<<i))) {\n c++;\n }\n }\n return c;\n }\n};", "memory": "8500" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n int charcnt = 0;\n for (char c = 'a'; c <= 'z'; c++) {\n char lower = c;\n char upper = c - 32;\n if (word.find(lower) != string::npos && word.find(upper) != string::npos) {\n charcnt++;\n }\n }\n return charcnt;\n }\n};", "memory": "8500" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n int or_lower = 0, or_upper = 0;\n for (char ch : word) {\n if (islower(ch)) {\n or_lower |= (1 << (ch - 'a'));\n } else {\n or_upper |= (1 << (ch - 'A'));\n }\n }\n\n int ans = 0;\n for (int i = 0; i < 26; i++) {\n if (or_lower & (1 << i) && or_upper & (1 << i))\n ans++;\n }\n\n return ans;\n }\n};", "memory": "8600" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
0
{ "code": "\n//M-1 --> brute force\n\n/*\nclass Solution {\npublic:\n int numberOfSpecialChars(string word) {\n \n unordered_map<int, unordered_set<char>> mp;\n\n int ans=0;\n for(auto w: word)\n {\n mp[tolower(w)-'a'].insert(w);\n }\n for(auto it: mp) if(it.second.size()>1) ans++;\n return ans;\n\n }\n};\n*/\n\n\n//M-2 --> optimal\n/*\nclass Solution {\npublic:\n int numberOfSpecialChars(string word) {\n \n vector<int> vis1(26,0);\n vector<int> vis2(26,0);\n\n int ans=0;\n for(auto w: word)\n {\n if(w<'a') vis2[tolower(w)-'a']++;\n else vis1[w-'a']++;\n }\n for(int i=0; i<26; i++) if(vis1[i]>0 && vis2[i]>0) ans++;\n return ans;\n\n }\n};\n*/\n\n\n\n// M-3--> bit manp.\n\nclass Solution {\npublic:\n int numberOfSpecialChars(string word) {\n int or_lower = 0, or_upper = 0;\n for(char ch : word){\n if(islower(ch)){\n or_lower |= (1<< (ch-'a'));\n }else{\n or_upper |= (1<< (ch-'A'));\n }\n }\n \n int ans=0;\n for(int i=0;i<26;i++){\n if(or_lower & (1<<i) && or_upper & (1<<i)) ans++;\n }\n \n return ans;\n }\n};", "memory": "8600" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n int lower=0,upper=0; // store data in bit\n for(char c : word){\n if(islower(c)) lower |= (1 << (c-'a'));\n else upper |= (1 << (c-'A'));\n }\n int count=0;\n for(int i=26;i>=0;i--)\n count += (lower & (1 << i)) and (upper & (1 << i));\n return count;\n }\n};", "memory": "8700" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n \n vector<char>Low(26,0),Up(26,0);\n int Ans=0;\n \n for(int i=0;i<word.size();i++)\n {\n if(word[i]>='a' and word[i]<='z')\n Low[word[i]-'a']++;\n else\n Up[word[i]-'A']++;\n }\n \n for(int i=0;i<26;i++)\n {\n if(Low[i]>0 and Up[i]>0)\n Ans++;\n }\n \n return Ans;\n }\n};", "memory": "8800" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n bool upper[26] = {}, lower[26] = {};\n for (auto c : word) {\n if (isupper(c)) {\n upper[c - 'A'] = true;\n }\n if (islower(c)) {\n lower[c - 'a'] = true;\n }\n }\n int ans = 0;\n for (int i = 0; i < 26; ++i) {\n if (upper[i] && lower[i]) ans++;\n }\n return ans;\n }\n};", "memory": "8900" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n vector<int> bitmap(26,0);\n int cnt = 0;\n for(int i = 0;word[i];i++){\n char c = word[i];\n if(c >= 'a' && c <= 'z'){\n if((bitmap[c-'a'] & 1UL) == 0){\n bitmap[c-'a'] |= (1UL);\n if(bitmap[c-'a'] == 0b11) cnt++;\n }\n }\n else{\n if((bitmap[c-'A'] & (1UL<<1)) == 0){\n bitmap[c-'A'] |= (1UL<<1);\n if(bitmap[c-'A'] == 0b11) cnt++;\n }\n }\n }\n return cnt;\n }\n};", "memory": "8900" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n vector<int>lower(26),upper(26);\n for(auto it : word){\n if(islower(it)) lower[it-'a']++;\n else upper[it-'A']++;\n }\n int count=0;\n for(int i=0;i<26;i++)\n count += lower[i] and upper[i];\n return count;\n }\n};", "memory": "9000" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n vector<int> mas(26), minu(26);\n for (int i = 0; i < word.size(); i++) {\n if (!(word[i] - tolower(word[i]))) {\n minu[word[i] - 'a']++;\n } else {\n mas[word[i] - 'A']++;\n }\n }\n int ans = 0;\n for (int i = 0; i < 26; i++) {\n if (minu[i] && mas[i]) {\n ans++;\n }\n }\n return ans;\n }\n};", "memory": "9000" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n int n = word.size();\n vector<int> a(26, -1);\n vector<int> b(26, -1); \n for (int i = 0; i < n; i++) {\n char ch = word[i];\n if (islower(ch)) {\n a[ch - 'a'] = 1;\n } else if (isupper(ch)) {\n if (b[ch - 'A'] == -1) {\n b[ch - 'A'] = 1;\n }\n }\n }\n \n int ans = 0;\n for (int i = 0; i < 26; i++) {\n if(a[i]==1&&b[i]==1)ans++;\n }\n \n return ans;\n }\n};\n", "memory": "9100" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n\n int ch[26] = {0};\n int ch2[26] = {0};\n\n for(int i=0;i<word.size();i++){\n if(word[i]-'a' >=0 && word[i]-'a' <26)\n ch[word[i]-'a']++;\n if(word[i]-'A' >=0 && word[i]-'A'<26)\n ch2[word[i]-'A']++;\n }\n\n int count = 0;\n\n for(int i=0;i<26;i++){\n if(ch[i] !=0 && ch2[i] !=0) count++;\n }\n\n return count;\n \n }\n};", "memory": "9100" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n vector<int> lowerCase(26,0);\n vector<int> upperCase(26,0);\n for(auto &c: word){\n if(c-'a'>=0 && c-'a'<26){\n lowerCase[c-'a']=1;\n } else {\n upperCase[c-'A']=1;\n }\n }\n int ans=0;\n for(int i=0;i<26;i++)\n {\n if(lowerCase[i]!=0 && lowerCase[i]==upperCase[i]){\n ans++;\n }\n }\n return ans;\n }\n};", "memory": "9200" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
1
{ "code": "\n//M-1 --> brute force\n\n/*\nclass Solution {\npublic:\n int numberOfSpecialChars(string word) {\n \n unordered_map<int, unordered_set<char>> mp;\n\n int ans=0;\n for(auto w: word)\n {\n mp[tolower(w)-'a'].insert(w);\n }\n for(auto it: mp) if(it.second.size()>1) ans++;\n return ans;\n\n }\n};\n*/\n\n\n//M-2 --> optimal\n\nclass Solution {\npublic:\n int numberOfSpecialChars(string word) {\n \n vector<int> vis1(26,0);\n vector<int> vis2(26,0);\n\n int ans=0;\n for(auto w: word)\n {\n if(w<'a') vis2[tolower(w)-'a']++;\n else vis1[w-'a']++;\n }\n for(int i=0; i<26; i++) if(vis1[i]>0 && vis2[i]>0) ans++;\n return ans;\n\n }\n};", "memory": "9300" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n vector<vector<int>> cnt(2, vector<int>(26));\n for(auto& x : word) {\n int i = ('a' <= x) && (x <= 'z');\n if(i == 1) {\n cnt[i][x-'a']++;\n } else {\n cnt[i][x-'A']++;\n }\n }\n int res = 0;\n for(int i = 0; i < 26; ++i)\n res += cnt[0][i] && cnt[1][i];\n return res;\n }\n};", "memory": "9400" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool isspecial( string s , char ch )\n {\n if( isupper(ch) )\n {\n ch = tolower(ch) ; \n for( auto chr : s )\n {\n if( ch == chr )\n return true ; \n }\n }\n else if(islower(ch))\n {\n ch = toupper(ch) ; \n for( auto chr : s )\n {\n if(ch == chr)\n return true ; \n }\n }\n return false ; \n }\n\n int numberOfSpecialChars(string word) \n {\n int counter = 0 ; \n string checking = \"\" ; \n for( auto ch : word )\n {\n if( checking.find(tolower(ch)) != string::npos ) // already visited\n continue ; \n if(isspecial(word , ch))\n {\n checking += tolower(ch) ; \n counter++ ; \n }\n }\n\n return counter ; \n }\n};", "memory": "9500" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n int count = 0;\n set<char>st(word.begin(),word.end());\n\n // Loop through the set and count matching uppercase-lowercase pairs\n for(auto it : st){\n if((it >= 'a' && it <= 'z') && st.find(toupper(it)) != st.end()) {\n count++;\n } else if((it >= 'A' && it <= 'Z') && st.find(tolower(it)) != st.end()) {\n count++;\n }\n }\n\n // Since each pair is counted twice, divide by 2\n return count / 2;\n }\n};\n", "memory": "9500" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n set<char> s;\n set<char> found;\n\n int special = 0;\n\n for (int i = 0; i < word.size(); ++i) {\n if (s.find(tolower(word[i])) != s.end() && found.find(tolower(word[i])) == found.end()) {\n if (isupper(word[i])) {\n special++;\n found.insert(tolower(word[i]));\n }\n }\n else if (s.find(toupper(word[i])) != s.end() && found.find(tolower(word[i])) == found.end()) {\n if (islower(word[i])) {\n special++;\n found.insert(tolower(word[i]));\n }\n }\n else {\n s.insert(word[i]);\n }\n }\n\n return special;\n }\n};", "memory": "9600" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n vector<int>hash(26,0);\n set<char>se(word.begin(),word.end());\n for(auto it:se)\n {\n if(int(it)>=97)\n {\n hash[int(it)-97]++;\n }\n else{\n hash[int(it)-65]++;\n }\n }\n int cnt=0;\n for(int i=0;i<26;i++)\n {\n if(hash[i]==2)\n {\n cnt++;\n }\n }\n return cnt;\n }\n};", "memory": "9700" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n unordered_map<char,int>m;\n set<char>s;\n for(int i=0;i<word.length();i++)\n {\n char ch=word[i];\n if(ch>='a' && ch<='z')\n {\n m[ch]++;\n }\n }\n for(int i=0;i<word.length();i++)\n {\n char ch=word[i];\n if(ch>='A' && ch<='Z')\n {\n if(m.find(ch-'A'+'a')!=m.end())\n {\n \n s.insert(ch);\n }\n }\n }\n return s.size();\n }\n};", "memory": "10000" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n unordered_map<char, int> map;\n for(auto& c: word) {\n map[c]++;\n }\n int cnt = 0;\n for (int i = 0; i < 26; i++) {\n char c = 'a'+i;\n char c1 = 'A' + i;\n if (map.count(c) && map.count(c1)) cnt++;\n }\n return cnt;\n }\n};", "memory": "10100" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n unordered_map<char, int> map;\n for(auto& c: word) {\n map[c]++;\n }\n int cnt = 0;\n for (int i = 0; i < 26; i++) {\n char c = 'a'+i;\n char c1 = 'A' + i;\n if (map.count(c) && map.count(c1)) cnt++;\n }\n return cnt;\n }\n};", "memory": "10100" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n \n map<char,int>mp;\n int x=0;\n\n for(int i=0;i<word.size();i++)\n {\n mp[word[i]]++;\n }\n\n for(int i=0;i<word.size();i++)\n { char c;\n int d;\n if(int(word[i])>96)\n { d=int(word[i]);\n d=d-32;\n c=char(d);\n\n if(mp.find(c)!=mp.end() && mp[word[i]]!=0)\n { mp[word[i]]=0;\n x++;\n cout<<i<<endl;}\n }\n \n }\n return x;\n }\n \n};", "memory": "10200" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n unordered_map<int,char>mp;\n set<char>ch(word.begin(),word.end());\n for( auto c : ch)\n {\n c=tolower(c);\n mp[c]++;\n }\n int count=0;\n for(auto it : mp)\n {\n if(it.second >1)\n count++;\n }\n\n return count;\n \n }\n};", "memory": "10300" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n unordered_map<char,int>mp;\n set<char>ch(word.begin(),word.end());\n for( auto c : ch)\n {\n c=tolower(c);\n mp[c]++;\n }\n int count=0;\n for(auto it : mp)\n {\n if(it.second >1)\n count++;\n }\n\n return count;\n \n }\n};", "memory": "10300" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n map<char,int> presence;\n vector<int> visited(26,0);\n for(char c : word)\n {\n if(c>=97 && c<=122) \n {\n presence[c] = 1;\n visited[c-97] = 1;\n }\n }\n int count = 0;\n for(char c : word)\n {\n if(c>=65 && c<=90 && presence[char(c+32)] == 1 && visited[c+32-97]) \n {\n count++;\n visited[c+32-97] = 0;\n }\n }\n return count;\n }\n};", "memory": "10400" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word)\n {\n // A letter is special if it appears both in lowercase and uppercase \n //if it exists increment the counter by 1 else return 0 ;\n unordered_set<char> lowercase;\n unordered_set<char> uppercase;\n int specialCount = 0; \n\n for(char c:word)\n {\n if(c>='a' && c<='z')\n {\n lowercase.insert(c);\n } \n else if(c>='A' && c<='Z')\n {\n uppercase.insert(c); \n }\n }\n for(char c : lowercase)\n {\n if(uppercase.count(c-'a'+'A'))\n {\n specialCount++;\n }\n }\n\n return specialCount++;\n \n }\n};\n", "memory": "10500" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n map<char,int> mp1;\n map<char,int>mp2;\n int cnt=0;\n for(auto ch:word){\n if(ch>=65 && ch<=90)mp1[ch]++;\n if(ch>=97 && ch<=122)mp2[ch]++;\n } \n for(auto ch:mp2){\n \n if(mp1.find(ch.first-32)!=mp1.end())cnt++;\n \n }\n return cnt;\n }\n};", "memory": "10500" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word)\n {\n // A letter is special if it appears both in lowercase and uppercase \n //if it exists increment the counter by 1 else return 0 ;\n unordered_set<char> lowercase;\n unordered_set<char> uppercase;\n int specialCount = 0; \n\n for(char c:word)\n {\n if(c>='a' && c<='z')\n {\n lowercase.insert(c);\n } \n else if(c>='A' && c<='Z')\n {\n uppercase.insert(c); \n }\n }\n for(char c : lowercase)\n {\n if(uppercase.count(c-'a'+'A'))\n {\n specialCount++;\n }\n }\n\n return specialCount++;\n \n }\n};\n", "memory": "10600" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n unordered_set<char> lower;\n unordered_set<char> upper;\n for (const auto& w : word) {\n if (isupper(w)) { upper.insert(tolower(w)); }\n if (islower(w)) { lower.insert(w); }\n }\n int ans = 0;\n for (const auto& c : lower) {\n if (upper.contains(c)) { ans++; }\n }\n return ans;\n }\n};", "memory": "10600" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string w) {\n int ans = 0;\n unordered_map<char,int>a;\n unordered_set<char>s(w.begin(),w.end());\n for(char y:s){\n y=toupper(y);\n a[y]++;\n }\n for(auto x:a){\n if(x.second==2){\n ans++;\n }\n }\n return ans;\n }\n};\n", "memory": "10700" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n unordered_map<char, bool> mp;\n vector<bool>ans(26,false);\n int count = 0;\n // ab B C ab\n for (char ch: word) {\n if ('a' <= ch && ch <= 'z') {\n if (mp[toupper(ch)]) {\n ans[ch-'a'] = true;\n }\n }\n else { //upper\n if (mp[tolower(ch)]) {\n ans[tolower(ch)-'a'] = true;\n }\n // mp[]\n }\n mp[ch] = true;\n }\n for (int i = 0; i < 26; i++) {\n if(ans[i]) count++;\n }\n return count;\n }\n};", "memory": "10800" }
3,408
<p>You are given a string <code>word</code>. A letter is called <strong>special</strong> if it appears <strong>both</strong> in lowercase and uppercase in <code>word</code>.</p> <p>Return the number of<em> </em><strong>special</strong> letters in<em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;aaAbcBC&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The special characters in <code>word</code> are <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No character in <code>word</code> appears in uppercase.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">word = &quot;abBCab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only special character in <code>word</code> is <code>&#39;b&#39;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word.length &lt;= 50</code></li> <li><code>word</code> consists of only lowercase and uppercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int numberOfSpecialChars(string word) {\n map<char , bool> mp , mp1;\n int count = 0;\n for(auto ch : word){\n if(ch >= 'a' && ch <= 'z'){\n mp[ch] = true;\n }else{\n mp1[ch] = true;\n }\n }\n\n for(auto x : mp1){\n if(mp[char(x.first + 32)] == true){\n count++;\n }\n }\n\n return count;\n }\n};", "memory": "10900" }
3,568
<p>You are given three <strong>positive</strong> integers <code>num1</code>, <code>num2</code>, and <code>num3</code>.</p> <p>The <code>key</code> of <code>num1</code>, <code>num2</code>, and <code>num3</code> is defined as a four-digit number such that:</p> <ul> <li>Initially, if any number has <strong>less than</strong> four digits, it is padded with <strong>leading zeros</strong>.</li> <li>The <code>i<sup>th</sup></code> digit (<code>1 &lt;= i &lt;= 4</code>) of the <code>key</code> is generated by taking the <strong>smallest</strong> digit among the <code>i<sup>th</sup></code> digits of <code>num1</code>, <code>num2</code>, and <code>num3</code>.</li> </ul> <p>Return the <code>key</code> of the three numbers <strong>without</strong> leading zeros (<em>if any</em>).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 10, num3 = 1000</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>On padding, <code>num1</code> becomes <code>&quot;0001&quot;</code>, <code>num2</code> becomes <code>&quot;0010&quot;</code>, and <code>num3</code> remains <code>&quot;1000&quot;</code>.</p> <ul> <li>The <code>1<sup>st</sup></code> digit of the <code>key</code> is <code>min(0, 0, 1)</code>.</li> <li>The <code>2<sup>nd</sup></code> digit of the <code>key</code> is <code>min(0, 0, 0)</code>.</li> <li>The <code>3<sup>rd</sup></code> digit of the <code>key</code> is <code>min(0, 1, 0)</code>.</li> <li>The <code>4<sup>th</sup></code> digit of the <code>key</code> is <code>min(1, 0, 0)</code>.</li> </ul> <p>Hence, the <code>key</code> is <code>&quot;0000&quot;</code>, i.e. 0.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 987, num2 = 879, num3 = 798</span></p> <p><strong>Output:</strong> <span class="example-io">777</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 2, num3 = 3</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num1, num2, num3 &lt;= 9999</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int generateKey(int num1, int num2, int num3) {\n int res = 0, mult = 1;\n while(true) {\n if(!num1 and !num2 and !num3) break;\n res = min({num1 % 10, num2 % 10, num3 % 10}) * mult + res;\n num1 /= 10; num2 /= 10; num3 /= 10;\n mult *= 10;\n }\n return res;\n }\n};", "memory": "7900" }
3,568
<p>You are given three <strong>positive</strong> integers <code>num1</code>, <code>num2</code>, and <code>num3</code>.</p> <p>The <code>key</code> of <code>num1</code>, <code>num2</code>, and <code>num3</code> is defined as a four-digit number such that:</p> <ul> <li>Initially, if any number has <strong>less than</strong> four digits, it is padded with <strong>leading zeros</strong>.</li> <li>The <code>i<sup>th</sup></code> digit (<code>1 &lt;= i &lt;= 4</code>) of the <code>key</code> is generated by taking the <strong>smallest</strong> digit among the <code>i<sup>th</sup></code> digits of <code>num1</code>, <code>num2</code>, and <code>num3</code>.</li> </ul> <p>Return the <code>key</code> of the three numbers <strong>without</strong> leading zeros (<em>if any</em>).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 10, num3 = 1000</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>On padding, <code>num1</code> becomes <code>&quot;0001&quot;</code>, <code>num2</code> becomes <code>&quot;0010&quot;</code>, and <code>num3</code> remains <code>&quot;1000&quot;</code>.</p> <ul> <li>The <code>1<sup>st</sup></code> digit of the <code>key</code> is <code>min(0, 0, 1)</code>.</li> <li>The <code>2<sup>nd</sup></code> digit of the <code>key</code> is <code>min(0, 0, 0)</code>.</li> <li>The <code>3<sup>rd</sup></code> digit of the <code>key</code> is <code>min(0, 1, 0)</code>.</li> <li>The <code>4<sup>th</sup></code> digit of the <code>key</code> is <code>min(1, 0, 0)</code>.</li> </ul> <p>Hence, the <code>key</code> is <code>&quot;0000&quot;</code>, i.e. 0.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 987, num2 = 879, num3 = 798</span></p> <p><strong>Output:</strong> <span class="example-io">777</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 2, num3 = 3</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num1, num2, num3 &lt;= 9999</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int generateKey(int num1, int num2, int num3) {\n int ans=0,i=1;\n // int n1=num1%10;\n // int n2=num2%10;\n // int n3=num3%10;\n while(num1 || num2 || num3){\n int dig=min({num1%10,num2%10,num3%10});\n ans=ans+dig*i;\n i*=10;\n num1/=10;\n num2/=10;\n num3/=10;\n }\n return ans;\n \n }\n};", "memory": "8000" }
3,568
<p>You are given three <strong>positive</strong> integers <code>num1</code>, <code>num2</code>, and <code>num3</code>.</p> <p>The <code>key</code> of <code>num1</code>, <code>num2</code>, and <code>num3</code> is defined as a four-digit number such that:</p> <ul> <li>Initially, if any number has <strong>less than</strong> four digits, it is padded with <strong>leading zeros</strong>.</li> <li>The <code>i<sup>th</sup></code> digit (<code>1 &lt;= i &lt;= 4</code>) of the <code>key</code> is generated by taking the <strong>smallest</strong> digit among the <code>i<sup>th</sup></code> digits of <code>num1</code>, <code>num2</code>, and <code>num3</code>.</li> </ul> <p>Return the <code>key</code> of the three numbers <strong>without</strong> leading zeros (<em>if any</em>).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 10, num3 = 1000</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>On padding, <code>num1</code> becomes <code>&quot;0001&quot;</code>, <code>num2</code> becomes <code>&quot;0010&quot;</code>, and <code>num3</code> remains <code>&quot;1000&quot;</code>.</p> <ul> <li>The <code>1<sup>st</sup></code> digit of the <code>key</code> is <code>min(0, 0, 1)</code>.</li> <li>The <code>2<sup>nd</sup></code> digit of the <code>key</code> is <code>min(0, 0, 0)</code>.</li> <li>The <code>3<sup>rd</sup></code> digit of the <code>key</code> is <code>min(0, 1, 0)</code>.</li> <li>The <code>4<sup>th</sup></code> digit of the <code>key</code> is <code>min(1, 0, 0)</code>.</li> </ul> <p>Hence, the <code>key</code> is <code>&quot;0000&quot;</code>, i.e. 0.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 987, num2 = 879, num3 = 798</span></p> <p><strong>Output:</strong> <span class="example-io">777</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 2, num3 = 3</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num1, num2, num3 &lt;= 9999</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int generateKey(int num1, int num2, int num3) {\n int t=4,ten=1,ans=0;\n while(t--){\n int o=num1%10;\n int s=num2%10;\n int t=num3%10;\n int temp=min(o,min(s,t))*ten;\n num1=num1/10;\n num2=num2/10;\n num3=num3/10;\n ten=ten*10;\n ans+=temp;\n }\n return ans;\n }\n};", "memory": "8000" }
3,568
<p>You are given three <strong>positive</strong> integers <code>num1</code>, <code>num2</code>, and <code>num3</code>.</p> <p>The <code>key</code> of <code>num1</code>, <code>num2</code>, and <code>num3</code> is defined as a four-digit number such that:</p> <ul> <li>Initially, if any number has <strong>less than</strong> four digits, it is padded with <strong>leading zeros</strong>.</li> <li>The <code>i<sup>th</sup></code> digit (<code>1 &lt;= i &lt;= 4</code>) of the <code>key</code> is generated by taking the <strong>smallest</strong> digit among the <code>i<sup>th</sup></code> digits of <code>num1</code>, <code>num2</code>, and <code>num3</code>.</li> </ul> <p>Return the <code>key</code> of the three numbers <strong>without</strong> leading zeros (<em>if any</em>).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 10, num3 = 1000</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>On padding, <code>num1</code> becomes <code>&quot;0001&quot;</code>, <code>num2</code> becomes <code>&quot;0010&quot;</code>, and <code>num3</code> remains <code>&quot;1000&quot;</code>.</p> <ul> <li>The <code>1<sup>st</sup></code> digit of the <code>key</code> is <code>min(0, 0, 1)</code>.</li> <li>The <code>2<sup>nd</sup></code> digit of the <code>key</code> is <code>min(0, 0, 0)</code>.</li> <li>The <code>3<sup>rd</sup></code> digit of the <code>key</code> is <code>min(0, 1, 0)</code>.</li> <li>The <code>4<sup>th</sup></code> digit of the <code>key</code> is <code>min(1, 0, 0)</code>.</li> </ul> <p>Hence, the <code>key</code> is <code>&quot;0000&quot;</code>, i.e. 0.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 987, num2 = 879, num3 = 798</span></p> <p><strong>Output:</strong> <span class="example-io">777</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 2, num3 = 3</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num1, num2, num3 &lt;= 9999</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int generateKey(int num1, int num2, int num3) {\n\n int ans = 0;\n int ten = 1;\n while (num1 && num2 && num3)\n {\n int a = num1%10;\n int b = num2%10;\n int c= num3%10;\n\n int temp = min(min(a,b),min(b,c));\n num1 = num1/10;\n num2 = num2/10;\n num3 = num3/10;\n\n ans = ans + ten*temp;\n ten = ten*10;\n }\n return ans;\n }\n};", "memory": "8100" }
3,568
<p>You are given three <strong>positive</strong> integers <code>num1</code>, <code>num2</code>, and <code>num3</code>.</p> <p>The <code>key</code> of <code>num1</code>, <code>num2</code>, and <code>num3</code> is defined as a four-digit number such that:</p> <ul> <li>Initially, if any number has <strong>less than</strong> four digits, it is padded with <strong>leading zeros</strong>.</li> <li>The <code>i<sup>th</sup></code> digit (<code>1 &lt;= i &lt;= 4</code>) of the <code>key</code> is generated by taking the <strong>smallest</strong> digit among the <code>i<sup>th</sup></code> digits of <code>num1</code>, <code>num2</code>, and <code>num3</code>.</li> </ul> <p>Return the <code>key</code> of the three numbers <strong>without</strong> leading zeros (<em>if any</em>).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 10, num3 = 1000</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>On padding, <code>num1</code> becomes <code>&quot;0001&quot;</code>, <code>num2</code> becomes <code>&quot;0010&quot;</code>, and <code>num3</code> remains <code>&quot;1000&quot;</code>.</p> <ul> <li>The <code>1<sup>st</sup></code> digit of the <code>key</code> is <code>min(0, 0, 1)</code>.</li> <li>The <code>2<sup>nd</sup></code> digit of the <code>key</code> is <code>min(0, 0, 0)</code>.</li> <li>The <code>3<sup>rd</sup></code> digit of the <code>key</code> is <code>min(0, 1, 0)</code>.</li> <li>The <code>4<sup>th</sup></code> digit of the <code>key</code> is <code>min(1, 0, 0)</code>.</li> </ul> <p>Hence, the <code>key</code> is <code>&quot;0000&quot;</code>, i.e. 0.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 987, num2 = 879, num3 = 798</span></p> <p><strong>Output:</strong> <span class="example-io">777</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 2, num3 = 3</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num1, num2, num3 &lt;= 9999</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int generateKey(int num1, int num2, int num3) {\n int div = 1000;\n int res = 0;\n\n for(int i = 0; i<4; i++){\n res = res*10 + min(num1/div, min(num2/div, num3/div));\n\n num1 %= div;\n num2 %= div;\n num3 %= div;\n \n div /= 10;\n }\n return res;\n }\n};", "memory": "8100" }
3,568
<p>You are given three <strong>positive</strong> integers <code>num1</code>, <code>num2</code>, and <code>num3</code>.</p> <p>The <code>key</code> of <code>num1</code>, <code>num2</code>, and <code>num3</code> is defined as a four-digit number such that:</p> <ul> <li>Initially, if any number has <strong>less than</strong> four digits, it is padded with <strong>leading zeros</strong>.</li> <li>The <code>i<sup>th</sup></code> digit (<code>1 &lt;= i &lt;= 4</code>) of the <code>key</code> is generated by taking the <strong>smallest</strong> digit among the <code>i<sup>th</sup></code> digits of <code>num1</code>, <code>num2</code>, and <code>num3</code>.</li> </ul> <p>Return the <code>key</code> of the three numbers <strong>without</strong> leading zeros (<em>if any</em>).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 10, num3 = 1000</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>On padding, <code>num1</code> becomes <code>&quot;0001&quot;</code>, <code>num2</code> becomes <code>&quot;0010&quot;</code>, and <code>num3</code> remains <code>&quot;1000&quot;</code>.</p> <ul> <li>The <code>1<sup>st</sup></code> digit of the <code>key</code> is <code>min(0, 0, 1)</code>.</li> <li>The <code>2<sup>nd</sup></code> digit of the <code>key</code> is <code>min(0, 0, 0)</code>.</li> <li>The <code>3<sup>rd</sup></code> digit of the <code>key</code> is <code>min(0, 1, 0)</code>.</li> <li>The <code>4<sup>th</sup></code> digit of the <code>key</code> is <code>min(1, 0, 0)</code>.</li> </ul> <p>Hence, the <code>key</code> is <code>&quot;0000&quot;</code>, i.e. 0.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 987, num2 = 879, num3 = 798</span></p> <p><strong>Output:</strong> <span class="example-io">777</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 2, num3 = 3</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num1, num2, num3 &lt;= 9999</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int generateKey(int num1, int num2, int num3) {\n int res = 0;\n int prod = 1;\n for (int i = 0; i < 4; i++) {\n res += min(num1 % 10, min(num2 % 10, num3 % 10)) * prod;\n prod *= 10;\n num1 /= 10, num2 /= 10, num3 /= 10;\n }\n return res; \n }\n};", "memory": "8200" }
3,568
<p>You are given three <strong>positive</strong> integers <code>num1</code>, <code>num2</code>, and <code>num3</code>.</p> <p>The <code>key</code> of <code>num1</code>, <code>num2</code>, and <code>num3</code> is defined as a four-digit number such that:</p> <ul> <li>Initially, if any number has <strong>less than</strong> four digits, it is padded with <strong>leading zeros</strong>.</li> <li>The <code>i<sup>th</sup></code> digit (<code>1 &lt;= i &lt;= 4</code>) of the <code>key</code> is generated by taking the <strong>smallest</strong> digit among the <code>i<sup>th</sup></code> digits of <code>num1</code>, <code>num2</code>, and <code>num3</code>.</li> </ul> <p>Return the <code>key</code> of the three numbers <strong>without</strong> leading zeros (<em>if any</em>).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 10, num3 = 1000</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>On padding, <code>num1</code> becomes <code>&quot;0001&quot;</code>, <code>num2</code> becomes <code>&quot;0010&quot;</code>, and <code>num3</code> remains <code>&quot;1000&quot;</code>.</p> <ul> <li>The <code>1<sup>st</sup></code> digit of the <code>key</code> is <code>min(0, 0, 1)</code>.</li> <li>The <code>2<sup>nd</sup></code> digit of the <code>key</code> is <code>min(0, 0, 0)</code>.</li> <li>The <code>3<sup>rd</sup></code> digit of the <code>key</code> is <code>min(0, 1, 0)</code>.</li> <li>The <code>4<sup>th</sup></code> digit of the <code>key</code> is <code>min(1, 0, 0)</code>.</li> </ul> <p>Hence, the <code>key</code> is <code>&quot;0000&quot;</code>, i.e. 0.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 987, num2 = 879, num3 = 798</span></p> <p><strong>Output:</strong> <span class="example-io">777</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 2, num3 = 3</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num1, num2, num3 &lt;= 9999</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int generateKey(int num1, int num2, int num3) {\n \n int ans = 0 , temp =1 ; \n while( num1 > 0 || num2 > 0 || num3 > 0 )\n {\n int x = num1 % 10 ; \n num1 /= 10 ; \n int y = num2 % 10 ; \n num2 /= 10 ; \n int z = num3 % 10 ; \n num3 /= 10 ; \n\n int a = min ( x, min(y , z)) ; \n ans = a * temp + ans ; \n temp *= 10 ; \n }\n return ans ; \n }\n};", "memory": "8200" }
3,568
<p>You are given three <strong>positive</strong> integers <code>num1</code>, <code>num2</code>, and <code>num3</code>.</p> <p>The <code>key</code> of <code>num1</code>, <code>num2</code>, and <code>num3</code> is defined as a four-digit number such that:</p> <ul> <li>Initially, if any number has <strong>less than</strong> four digits, it is padded with <strong>leading zeros</strong>.</li> <li>The <code>i<sup>th</sup></code> digit (<code>1 &lt;= i &lt;= 4</code>) of the <code>key</code> is generated by taking the <strong>smallest</strong> digit among the <code>i<sup>th</sup></code> digits of <code>num1</code>, <code>num2</code>, and <code>num3</code>.</li> </ul> <p>Return the <code>key</code> of the three numbers <strong>without</strong> leading zeros (<em>if any</em>).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 10, num3 = 1000</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>On padding, <code>num1</code> becomes <code>&quot;0001&quot;</code>, <code>num2</code> becomes <code>&quot;0010&quot;</code>, and <code>num3</code> remains <code>&quot;1000&quot;</code>.</p> <ul> <li>The <code>1<sup>st</sup></code> digit of the <code>key</code> is <code>min(0, 0, 1)</code>.</li> <li>The <code>2<sup>nd</sup></code> digit of the <code>key</code> is <code>min(0, 0, 0)</code>.</li> <li>The <code>3<sup>rd</sup></code> digit of the <code>key</code> is <code>min(0, 1, 0)</code>.</li> <li>The <code>4<sup>th</sup></code> digit of the <code>key</code> is <code>min(1, 0, 0)</code>.</li> </ul> <p>Hence, the <code>key</code> is <code>&quot;0000&quot;</code>, i.e. 0.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 987, num2 = 879, num3 = 798</span></p> <p><strong>Output:</strong> <span class="example-io">777</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 2, num3 = 3</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num1, num2, num3 &lt;= 9999</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int generateKey(int num1, int num2, int num3) {\n int key{0};\n \n int div = 1000;\n for(int i{0}; i<4; ++i)\n {\n key = key*10 + min(num1/div, min(num2/div, num3/div));\n\n num1 = num1%div;\n num2 = num2%div;\n num3 = num3%div;\n div = div/10; \n }\n return key;\n\n\n }\n};", "memory": "8300" }
3,568
<p>You are given three <strong>positive</strong> integers <code>num1</code>, <code>num2</code>, and <code>num3</code>.</p> <p>The <code>key</code> of <code>num1</code>, <code>num2</code>, and <code>num3</code> is defined as a four-digit number such that:</p> <ul> <li>Initially, if any number has <strong>less than</strong> four digits, it is padded with <strong>leading zeros</strong>.</li> <li>The <code>i<sup>th</sup></code> digit (<code>1 &lt;= i &lt;= 4</code>) of the <code>key</code> is generated by taking the <strong>smallest</strong> digit among the <code>i<sup>th</sup></code> digits of <code>num1</code>, <code>num2</code>, and <code>num3</code>.</li> </ul> <p>Return the <code>key</code> of the three numbers <strong>without</strong> leading zeros (<em>if any</em>).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 10, num3 = 1000</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>On padding, <code>num1</code> becomes <code>&quot;0001&quot;</code>, <code>num2</code> becomes <code>&quot;0010&quot;</code>, and <code>num3</code> remains <code>&quot;1000&quot;</code>.</p> <ul> <li>The <code>1<sup>st</sup></code> digit of the <code>key</code> is <code>min(0, 0, 1)</code>.</li> <li>The <code>2<sup>nd</sup></code> digit of the <code>key</code> is <code>min(0, 0, 0)</code>.</li> <li>The <code>3<sup>rd</sup></code> digit of the <code>key</code> is <code>min(0, 1, 0)</code>.</li> <li>The <code>4<sup>th</sup></code> digit of the <code>key</code> is <code>min(1, 0, 0)</code>.</li> </ul> <p>Hence, the <code>key</code> is <code>&quot;0000&quot;</code>, i.e. 0.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 987, num2 = 879, num3 = 798</span></p> <p><strong>Output:</strong> <span class="example-io">777</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 2, num3 = 3</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num1, num2, num3 &lt;= 9999</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int generateKey(int num1, int num2, int num3) {\n int res=0;\n for(int i=0;i<4;i++){\n int a=num1%10;\n int b=num2%10;\n int c=num3%10;\n int ans=min(a,b);\n ans=min(ans,c);\n res+=((pow(10,i)*ans));\n num1=num1/10;\n num2=num2/10;\n num3=num3/10;\n }\n return res;\n }\n};", "memory": "8300" }
3,568
<p>You are given three <strong>positive</strong> integers <code>num1</code>, <code>num2</code>, and <code>num3</code>.</p> <p>The <code>key</code> of <code>num1</code>, <code>num2</code>, and <code>num3</code> is defined as a four-digit number such that:</p> <ul> <li>Initially, if any number has <strong>less than</strong> four digits, it is padded with <strong>leading zeros</strong>.</li> <li>The <code>i<sup>th</sup></code> digit (<code>1 &lt;= i &lt;= 4</code>) of the <code>key</code> is generated by taking the <strong>smallest</strong> digit among the <code>i<sup>th</sup></code> digits of <code>num1</code>, <code>num2</code>, and <code>num3</code>.</li> </ul> <p>Return the <code>key</code> of the three numbers <strong>without</strong> leading zeros (<em>if any</em>).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 10, num3 = 1000</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>On padding, <code>num1</code> becomes <code>&quot;0001&quot;</code>, <code>num2</code> becomes <code>&quot;0010&quot;</code>, and <code>num3</code> remains <code>&quot;1000&quot;</code>.</p> <ul> <li>The <code>1<sup>st</sup></code> digit of the <code>key</code> is <code>min(0, 0, 1)</code>.</li> <li>The <code>2<sup>nd</sup></code> digit of the <code>key</code> is <code>min(0, 0, 0)</code>.</li> <li>The <code>3<sup>rd</sup></code> digit of the <code>key</code> is <code>min(0, 1, 0)</code>.</li> <li>The <code>4<sup>th</sup></code> digit of the <code>key</code> is <code>min(1, 0, 0)</code>.</li> </ul> <p>Hence, the <code>key</code> is <code>&quot;0000&quot;</code>, i.e. 0.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 987, num2 = 879, num3 = 798</span></p> <p><strong>Output:</strong> <span class="example-io">777</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 2, num3 = 3</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num1, num2, num3 &lt;= 9999</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int generateKey(int num1, int num2, int num3) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n int res=0;\n int mult=1;\n while(1){\n if(!num1 && !num2 && !num3)break;\n else{\n res+=min(num1%10,min(num2%10,num3%10))*mult;\n }\n num1/=10;\n num2/=10;\n num3/=10;\n mult*=10;\n }\n return res;\n }\n};\n// class Solution {\n// public:\n// int generateKey(int num1, int num2, int num3) {\n// ios_base::sync_with_stdio(false);\n// cin.tie(NULL);\n// string s1,s2,s3;\n// s1=to_string(num1);\n// s2=to_string(num2);\n// s3=to_string(num3);\n// while(s1.size()<4)s1=\"0\"+s1;\n// while(s2.size()<4)s2=\"0\"+s2;\n// while(s3.size()<4)s3=\"0\"+s3;\n// string key=\"\";\n// key[0]=min(s1[0],min(s2[0],s3[0]));\n// key[1]=min(s1[1],min(s2[1],s3[1]));\n// key[2]=min(s1[2],min(s2[2],s3[2]));\n// key[3]=min(s1[3],min(s2[3],s3[3]));\n// return stoi(key);\n// }\n// };", "memory": "8400" }
3,568
<p>You are given three <strong>positive</strong> integers <code>num1</code>, <code>num2</code>, and <code>num3</code>.</p> <p>The <code>key</code> of <code>num1</code>, <code>num2</code>, and <code>num3</code> is defined as a four-digit number such that:</p> <ul> <li>Initially, if any number has <strong>less than</strong> four digits, it is padded with <strong>leading zeros</strong>.</li> <li>The <code>i<sup>th</sup></code> digit (<code>1 &lt;= i &lt;= 4</code>) of the <code>key</code> is generated by taking the <strong>smallest</strong> digit among the <code>i<sup>th</sup></code> digits of <code>num1</code>, <code>num2</code>, and <code>num3</code>.</li> </ul> <p>Return the <code>key</code> of the three numbers <strong>without</strong> leading zeros (<em>if any</em>).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 10, num3 = 1000</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>On padding, <code>num1</code> becomes <code>&quot;0001&quot;</code>, <code>num2</code> becomes <code>&quot;0010&quot;</code>, and <code>num3</code> remains <code>&quot;1000&quot;</code>.</p> <ul> <li>The <code>1<sup>st</sup></code> digit of the <code>key</code> is <code>min(0, 0, 1)</code>.</li> <li>The <code>2<sup>nd</sup></code> digit of the <code>key</code> is <code>min(0, 0, 0)</code>.</li> <li>The <code>3<sup>rd</sup></code> digit of the <code>key</code> is <code>min(0, 1, 0)</code>.</li> <li>The <code>4<sup>th</sup></code> digit of the <code>key</code> is <code>min(1, 0, 0)</code>.</li> </ul> <p>Hence, the <code>key</code> is <code>&quot;0000&quot;</code>, i.e. 0.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 987, num2 = 879, num3 = 798</span></p> <p><strong>Output:</strong> <span class="example-io">777</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 2, num3 = 3</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num1, num2, num3 &lt;= 9999</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int generateKey(int num1, int num2, int num3) {\n int a = 9, b = 9, c = 9, d = 9;\n for(int x : {num1, num2, num3})\n {\n a = min(a, x / 1000);\n b = min(b, (x / 100) % 10);\n c = min(c, (x / 10) % 10);\n d = min(d, x % 10);\n }\n return a * 1000 + b * 100 + c * 10 + d;\n }\n};", "memory": "8400" }
3,568
<p>You are given three <strong>positive</strong> integers <code>num1</code>, <code>num2</code>, and <code>num3</code>.</p> <p>The <code>key</code> of <code>num1</code>, <code>num2</code>, and <code>num3</code> is defined as a four-digit number such that:</p> <ul> <li>Initially, if any number has <strong>less than</strong> four digits, it is padded with <strong>leading zeros</strong>.</li> <li>The <code>i<sup>th</sup></code> digit (<code>1 &lt;= i &lt;= 4</code>) of the <code>key</code> is generated by taking the <strong>smallest</strong> digit among the <code>i<sup>th</sup></code> digits of <code>num1</code>, <code>num2</code>, and <code>num3</code>.</li> </ul> <p>Return the <code>key</code> of the three numbers <strong>without</strong> leading zeros (<em>if any</em>).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 10, num3 = 1000</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>On padding, <code>num1</code> becomes <code>&quot;0001&quot;</code>, <code>num2</code> becomes <code>&quot;0010&quot;</code>, and <code>num3</code> remains <code>&quot;1000&quot;</code>.</p> <ul> <li>The <code>1<sup>st</sup></code> digit of the <code>key</code> is <code>min(0, 0, 1)</code>.</li> <li>The <code>2<sup>nd</sup></code> digit of the <code>key</code> is <code>min(0, 0, 0)</code>.</li> <li>The <code>3<sup>rd</sup></code> digit of the <code>key</code> is <code>min(0, 1, 0)</code>.</li> <li>The <code>4<sup>th</sup></code> digit of the <code>key</code> is <code>min(1, 0, 0)</code>.</li> </ul> <p>Hence, the <code>key</code> is <code>&quot;0000&quot;</code>, i.e. 0.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 987, num2 = 879, num3 = 798</span></p> <p><strong>Output:</strong> <span class="example-io">777</span></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num1 = 1, num2 = 2, num3 = 3</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num1, num2, num3 &lt;= 9999</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int generateKey(int num1, int num2, int num3) {\n vector<int> ans;\n while(num1 != 0 || num2 != 0 || num3 != 0){\n ans.push_back(min(num1 % 10, min(num2 % 10, num3 % 10)));\n num1 /= 10;\n num2 /= 10;\n num3 /= 10;\n }\n int key = 0;\n int n = ans.size();\n\n //reversing\n for(int i = n - 1; i >= 0; i--){\n key = key * 10 + ans[i];\n }\n return key;\n }\n};", "memory": "8500" }