id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nvoid solve(vector<int>&nums,int i,vector<int>&currSubset,vector<vector<int>>& subsets){\nif(i==nums.size()){\n subsets.push_back(currSubset);\n return;\n}\ncurrSubset.push_back(nums[i]);\nsolve(nums,i+1,currSubset,subsets);\ncurrSubset.pop_back();\nsolve(nums,i+1,currSubset,subsets);\n}\n int subsetXORSum(vector<int>& nums) {\n vector<vector<int>>subsets;\n vector<int>currSubset;\n solve(nums,0,currSubset,subsets);\n\n int result=0;\n for(vector<int>& subset:subsets){\n int Xor=0;\n for(int &num:subset){\n Xor^=num;\n }\n result+=Xor;\n }\n return result;\n }\n};", "memory": "19101" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void getAllSubsets(vector<int>& nums, vector<vector<int>>& subSets,\n vector<int>& subSet, int index) {\n if (nums.size() <= index) {\n subSets.push_back(subSet);\n return;\n }\n subSet.push_back(nums[index]);\n getAllSubsets(nums, subSets, subSet, index+1);\n subSet.pop_back();\n getAllSubsets(nums, subSets, subSet, index+1);\n }\n void getAllSubsets(vector<int>& nums, vector<vector<int>>& subSets) {\n vector<int> subSet;\n getAllSubsets(nums, subSets, subSet, 0);\n }\n int subsetXORSum(vector<int>& nums) {\n vector<vector<int>> subSets;\n getAllSubsets(nums, subSets);\n int sumXOR = 0;\n for (int i = 0; i < subSets.size(); i++) {\n int tempXOR = 0;\n for (int j = 0; j < subSets[i].size(); j++) {\n tempXOR ^= subSets[i][j];\n }\n sumXOR += tempXOR;\n }\n return sumXOR;\n }\n};", "memory": "19528" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void getAllSubsets(vector<int>& nums, vector<vector<int>>& subSets,\n vector<int>& subSet, int index) {\n if (nums.size() <= index) {\n subSets.push_back(subSet);\n return;\n }\n subSet.push_back(nums[index]);\n getAllSubsets(nums, subSets, subSet, index+1);\n subSet.pop_back();\n getAllSubsets(nums, subSets, subSet, index+1);\n }\n \n int subsetXORSum(vector<int>& nums) {\n vector<vector<int>> subSets;\n vector<int> subSet;\n getAllSubsets(nums, subSets, subSet, 0);\n int sumXOR = 0;\n for (int i = 0; i < subSets.size(); i++) {\n int tempXOR = 0;\n for (int j = 0; j < subSets[i].size(); j++) {\n tempXOR ^= subSets[i][j];\n }\n sumXOR += tempXOR;\n }\n return sumXOR;\n }\n};", "memory": "19528" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void helper(int i, vector<int>& nums,vector<int>& subset, vector<vector<int>>&subsets){\n if(i>=nums.size()){\n subsets.push_back(subset);\n return;\n }\n //with\n subset.push_back(nums[i]);\n helper(i+1,nums,subset,subsets);\n subset.pop_back();\n // without\n helper(i+1,nums,subset,subsets);\n }\n int subsetXORSum(vector<int>& nums) {\n int sum = 0;\n vector<vector<int>>subsets;\n vector<int>subset;\n helper(0,nums,subset, subsets);\n for(int i = 0; i<subsets.size();i++){\n int sumOfSubset = 0;\n for(int j = 0; j<subsets[i].size(); j++){\n sumOfSubset ^= subsets[i][j];\n }\n sum += sumOfSubset;\n }\n return sum;\n }\n};", "memory": "19956" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int>res;\n void subset(vector<int>&nums,vector<int>output,int i){\n int ans=0;\n for(int n:output){\n ans ^= n;\n \n\n }\n res.push_back(ans);\n for(int j=i;j<nums.size();j++){\n output.push_back(nums[j]);\n subset(nums,output,j + 1);\n output.pop_back();\n }\n }\n int subsetXORSum(vector<int>& nums) {\n vector<int>output;\n int i=0;\n int sum=0;\n subset(nums,output,i);\n for(int j=0;j<res.size();j++){\n sum+=res[j];\n }\n return sum;\n \n }\n};", "memory": "19956" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void calculate(vector<int>&nums,int index, vector<int>&v,vector<vector<int>>&v1)\n {\n if(index==nums.size())\n {\n v1.push_back(v);\n return;\n }\n \n // pickup case\n v.push_back(nums[index]);\n \n calculate(nums,index+1,v,v1);\n // not pickup case\n v.pop_back();\n calculate(nums,index+1,v,v1);\n \n }\n int subsetXORSum(vector<int>& nums) \n {\n int index=0;\n int xor_sum=0,ans=0;\n vector<int>v,res,val;\n vector<vector<int>>v1;\n calculate(nums,index,v,v1);\n for(int i=0;i<v1.size();i++)\n {\n for(int j=0;j<v1[i].size();j++)\n {\n xor_sum=xor_sum^v1[i][j];\n \n }\n res.push_back(xor_sum);\n xor_sum=0;\n }\n for(int i=0;i<res.size();i++)\n {\n ans=ans+res[i];\n }\n return ans;\n }\n};", "memory": "20383" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int sum = 0;\n void backtrack(int curIndex, vector<int>nums, int curXOR){\n int N = nums.size();\n for(int i = 0 ; i < 2; i++){\n curXOR = i == 0 ? curXOR : curXOR ^nums[curIndex];\n if(curIndex == N - 1){\n this->sum = this->sum + curXOR;\n } else {\n backtrack(curIndex+1, nums, curXOR );\n }\n }\n }\n int subsetXORSum(vector<int>& nums) {\n backtrack(0, nums, 0);\n return this->sum;\n }\n};", "memory": "20811" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int sum = 0;\n void backtrack(int curIndex, vector<int>nums, int curXOR){\n int N = nums.size();\n for(int i = 0 ; i < 2; i++){\n curXOR = i == 0 ? curXOR : curXOR ^nums[curIndex];\n if(curIndex == N - 1){\n int tempsum = curXOR;\n this->sum = this->sum + tempsum;\n } else {\n backtrack(curIndex+1, nums, curXOR );\n }\n }\n }\n int subsetXORSum(vector<int>& nums) {\n backtrack(0, nums, 0);\n return this->sum;\n }\n};", "memory": "21238" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int sum = 0;\n void backtrack(int curIndex, vector<int>nums, int curXOR){\n int N = nums.size();\n for(int i = 0 ; i < 2; i++){\n curXOR = i == 0 ? curXOR : curXOR ^nums[curIndex];\n if(curIndex == N - 1){\n this->sum = this->sum + curXOR;\n } else {\n backtrack(curIndex+1, nums, curXOR );\n }\n }\n }\n int subsetXORSum(vector<int>& nums) {\n backtrack(0, nums, 0);\n return this->sum;\n }\n};", "memory": "21238" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int result;\n\n void helper(int sum,vector<int> nums,int i)\n {\n if(i==nums.size()-1)\n {\n result+= sum + (sum^nums[i]);\n return;\n }\n\n helper(sum^nums[i],nums,i+1);\n helper(sum,nums,i+1);\n }\n\n\n int subsetXORSum(vector<int>& nums) {\n result = 0;\n helper(0,nums,0);\n return result;\n }\n};", "memory": "21666" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> arr;\n int rs = 0;\n int xor_fun(vector<int> arr) {\n int rs = 0;\n for(auto it: arr) rs^=it;\n return rs;\n }\n\n void back(int idx, vector<int> nums) {\n rs+=xor_fun(arr);\n for(int i = idx ; i <nums.size(); i++){\n arr.push_back(nums[i]);\n back(i+1, nums);\n arr.pop_back();\n }\n }\n int subsetXORSum(vector<int>& nums) {\n back(0, nums);\n return rs;\n }\n};", "memory": "22093" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int ans=0;\n int xorOfArray(vector<int>arr)\n{\n int xor_arr = 0;\n for (int i = 0; i < arr.size(); i++) {\n xor_arr = xor_arr ^ arr[i];\n }\n return xor_arr;\n}\n void subset(vector<int>&nums,vector<int> currVec,int index){\n ans+=xorOfArray(currVec);\n for(int i=index;i<nums.size();i++){\n currVec.push_back(nums[i]);\n subset(nums,currVec,i+1);\n currVec.pop_back();\n }\n }\n int subsetXORSum(vector<int>& nums) {\n vector<int> currVec;\n subset(nums,currVec,0);\n return ans;\n }\n};", "memory": "22521" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\n private:\n vector<vector<int>> ans;\n void rec(vector<int> &nums, int i, vector<int> &ds) {\n \n if(i==nums.size()){\n ans.push_back(ds);\n return;\n }\n rec(nums, i + 1, ds); \n ds.push_back(nums[i]); \n rec(nums, i + 1, ds); \n ds.pop_back(); \n return ;\n}\npublic:\n int subsetXORSum(vector<int>& nums) {\n vector<int> ds;\n rec(nums,0,ds);\n int a=0;\n for(int i=0;i<ans.size();i++){\n int x=0;\n for(int j=0;j<ans[i].size();j++){\n if(j==0){\n x=ans[i][j];\n }\n else{\n x=x^ans[i][j];\n }\n }\n a+=x;\n }\n return a;\n }\n};", "memory": "22948" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n void findsubsets(vector<int>& nums, vector<vector<int>>& subsets, int i, vector<int>& subset) {\n if (i == nums.size()) {\n subsets.push_back(subset);\n return;\n }\n findsubsets(nums, subsets, i + 1, subset);\n subset.push_back(nums[i]);\n findsubsets(nums, subsets, i + 1, subset);\n subset.pop_back();\n }\npublic:\n int subsetXORSum(vector<int>& nums) {\n int ans = 0;\n vector<vector<int>> subsets;\n vector<int> subset;\n findsubsets(nums, subsets, 0, subset);\n for(int i=0 ; i<subsets.size() ; i++){\n int x = 0;\n for(int j=0 ; j<subsets[i].size() ; j++){\n x = x^subsets[i][j];\n }\n ans += x;\n }\n return ans;\n }\n};", "memory": "23376" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\n private:\n vector<vector<int>> ans;\n void rec(vector<int> &nums, int i, vector<int> &ds) {\n \n if(i==nums.size()){\n ans.push_back(ds);\n return;\n }\n rec(nums, i + 1, ds); \n ds.push_back(nums[i]); \n rec(nums, i + 1, ds); \n ds.pop_back(); \n return ;\n}\npublic:\n int subsetXORSum(vector<int>& nums) {\n vector<int> ds;\n rec(nums,0,ds);\n int a=0;\n for(int i=0;i<ans.size();i++){\n int x=0;\n for(int j=0;j<ans[i].size();j++){\n if(j==0){\n x=ans[i][j];\n }\n else{\n x=x^ans[i][j];\n }\n }\n a+=x;\n }\n return a;\n }\n};", "memory": "23803" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n \n void Calculate(std::vector<std::vector<int>> &res, int &fres)\n {\n int result = 0;\n for(auto subsets: res)\n {\n int t = 0;\n for(int num: subsets)\n {\n\n t ^= num;\n }\n fres += t;\n \n \n }\n }\n\n void Util(vector<int> &nums,std::vector<int> &temp,vector<vector<int>> &res, int ind)\n {\n //cout<<ind<<endl;\n \n res.push_back(temp);\n for(int i=ind;i<nums.size();i++)\n {\n temp.push_back(nums[i]);\n \n Util(nums,temp,res,i+1);\n temp.pop_back();\n }\n } \n\n\n int subsetXORSum(vector<int>& nums) {\n\n int fres = 0;\n std::vector<int> temp;\n std::vector<vector<int>> res;\n Util(nums,temp,res, 0);\n Calculate(res,fres);\n return fres;\n \n }\n};", "memory": "24231" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n void calc(int ind,vector<int>& v1,vector<vector<int>>& res,vector<int>& nums){\n int n=nums.size();\n res.push_back(v1);\n\n for(int i=ind;i<n;i++){\n v1.push_back(nums[i]);\n calc(i+1,v1,res,nums);\n v1.pop_back();\n }\n }\n\n int subsetXORSum(vector<int>& nums) {\n vector<int> v1;\n vector<vector<int>> res;\n\n calc(0,v1,res,nums);\n int sum=0;\n for(auto it:res){\n int subxor=0;\n for(int i=0;i<it.size();i++){\n subxor^=it[i];\n }\n sum+=subxor;\n }\n return sum;\n }\n};", "memory": "24658" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void subsets(vector<int>& nums, int ind, vector<int>& v, vector<vector<int>>& ans)\n {\n if(ind >= nums.size()) \n {\n ans.push_back(v);\n return;\n }\n\n v.push_back(nums[ind]);\n subsets(nums, ind+1, v, ans);\n\n v.pop_back();\n subsets(nums, ind+1, v, ans);\n }\n int subsetXORSum(vector<int>& nums) {\n\n vector<int> v;\n vector<vector<int>> ans;\n\n subsets(nums, 0, v, ans);\n\n int sum=0;\n\n for(auto v:ans)\n {\n if(v.empty()) continue;\n int result = v[0];\n for(int i=1; i<v.size(); i++)\n {\n result = result^v[i];\n }\n\n sum += result;\n }\n\n return sum;\n \n }\n};", "memory": "25086" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void findAllSubset(int idx,vector<int>&nums,vector<vector<int>>&allSubset,vector<int>&result)\n {\n if(idx == nums.size())\n {\n allSubset.push_back(result);\n return;\n }\n // take\n result.push_back(nums[idx]);\n findAllSubset(idx+1,nums,allSubset,result);\n result.pop_back();\n // not take\n findAllSubset(idx+1,nums,allSubset,result);\n\n }\n int subsetXORSum(vector<int>& nums) {\n int ans = 0;\n vector<vector<int>>allSubset;\n vector<int>result;\n findAllSubset(0,nums,allSubset,result);\n for(auto subset : allSubset)\n {\n int subxor = 0;\n for(auto val : subset)\n subxor = subxor^val;\n ans = ans + subxor;\n }\n return ans; \n }\n};", "memory": "25513" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n int n = nums.size();\n int sum = 0;\n for(int i=0;i<(1<<n);i++){\n vector<int> temp;\n for(int j=0;j<n;j++){\n if(i&(1<<j)){\n temp.push_back(nums[j]);\n }\n }\n int xorans = 0;\n for(int k=0;k<temp.size();k++){\n xorans = xorans^temp[k];\n } \n sum = sum+xorans; \n }\n\n return sum;\n }\n};", "memory": "25513" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n int n = nums.size();\n int ans = 0;\n int totalSubsets = 1 << n;\n vector<vector<int>> subsets;\n for (int mask = 0; mask < totalSubsets; ++mask) {\n vector<int> subset;\n for (int i = 0; i < n; ++i) {\n if (mask & (1 << i)) { \n subset.push_back(nums[i]);\n }\n }\n \n int tmp = 0;\n for (int i = 0; i < subset.size(); i++) {\n tmp ^= subset[i];\n }\n ans += tmp;\n }\n return ans;\n }\n};", "memory": "25941" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n\n int sum=0;\n\n for(int i=0 ;i<(1<<nums.size());i++)\n {\n vector<int>v;\n for(int j=0;j<nums.size();j++)\n {\n if((i>>j)&1)\n {\n v.push_back( nums[j]);\n }\n }\n\n int n=0;\n\n for(int j=0;j<v.size();j++)\n {\n (n^=v[j]);\n\n }\n sum+=n;\n\n\n }\n\n return sum;\n }\n};", "memory": "25941" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": " void generateSubsets(vector<int>& nums, int index, vector<int>& currentSubset, vector<vector<int>>& allSubsets) {\n // Base case: if we've considered all elements\n if (index == nums.size()) {\n allSubsets.push_back(currentSubset);\n return;\n }\n\n // Include the current element\n currentSubset.push_back(nums[index]);\n generateSubsets(nums, index + 1, currentSubset, allSubsets);\n \n // Exclude the current element (backtrack)\n currentSubset.pop_back();\n generateSubsets(nums, index + 1, currentSubset, allSubsets);\n}\n\n// Function to generate all subsets of a given array\nvector<vector<int>> subsets(vector<int>& nums) {\n vector<vector<int>> allSubsets;\n vector<int> currentSubset;\n generateSubsets(nums, 0, currentSubset, allSubsets);\n return allSubsets;\n}\n\nclass Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n vector<int> subsetXOR;\n int it=0;\n int sum=0;\n \n\n\n\nvector<vector<int>> subsetsTotal = subsets(nums);\nfor (auto sub:subsetsTotal){\n if(sub.empty()){\n subsetXOR.emplace_back(0);\n }else{\n for(int i=0;i<sub.size();i++){\n int xorCurrent = it ^ sub[i];\n it=xorCurrent;\n }\n subsetXOR.emplace_back(it);\n it=0;\n }\n\n\n\n}\n\nfor(auto xorSum:subsetXOR){\n sum+=xorSum;\n}\n\nreturn sum;\n \n }\n};", "memory": "26368" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n\n\n\n\n void comb(vector<int> &a, vector<int> &temp, int i,vector<vector<int>> & ans){\n if (i<0){\n ans.push_back(temp);\n return;\n }\n comb(a,temp,i-1,ans);\n temp.push_back(a[i]);\n comb(a,temp,i-1,ans);\n temp.pop_back();\n }\n int subsetXORSum(vector<int>& a) {\n vector<int> temp;\n vector<vector<int>> ans;\n int n=a.size();\n comb(a,temp,n-1,ans);\n int sum=0;\n for (auto v:ans){\n int val=0;\n for (auto &x:v){\n val=val^x;\n }\n sum+=val;\n }\n return sum;\n }\n};", "memory": "28933" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\n public:\n int compute(const vector<int> &p){\n int xor_c=0;\n for(auto x:p){\n xor_c=xor_c^(x);\n }\n return xor_c;\n }\n public:\n void subset(int idx,vector<int>& nums,vector<vector<int>>&ans,vector<int>&v){\n\n if(idx==nums.size())\n {\n ans.push_back(v);\n return ;\n }\n subset(idx+1,nums,ans,v);\n v.push_back(nums[idx]);\n subset(idx+1,nums,ans,v);\n v.pop_back();\n }\npublic:\n int subsetXORSum(vector<int>& nums) {\n int n=nums.size();\n vector<vector<int>>ans;\n vector<int>v;\n subset(0,nums,ans,v); \n int res=0; \n for(auto it:ans){\n int x=compute(it);\n res=res+x;\n }\n return res;\n }\n};", "memory": "29361" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void XORSum(int idx,vector<int> &ds,int &res,vector<int> nums){\n \n if(idx == nums.size()){\n if(ds.size() != 0){\n int x = ds[0];\n for(int i = 1; i <ds.size(); i++){\n x ^= ds[i];\n }\n res += x;\n }\n return;\n }\n\n ds.push_back(nums[idx]);\n XORSum(idx+1,ds,res,nums);\n ds.pop_back();\n\n XORSum(idx+1,ds,res,nums);\n return;\n }\n int subsetXORSum(vector<int>& nums) {\n \n vector<int> ds;\n ds.clear();\n \n int res = 0;\n\n XORSum(0,ds,res,nums);\n return res;\n }\n};", "memory": "29788" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n int ans = 0;\n void rec(vector<int> nums,int index,int prev_xor){\n if(index == nums.size()){\n ans += prev_xor;\n return;\n }\n rec(nums,index+1,prev_xor^(nums[index]));\n rec(nums,index+1,prev_xor);\n return ;\n }\npublic:\n int subsetXORSum(vector<int>& nums) {\n rec(nums,0,0);\n return ans;\n }\n};", "memory": "30216" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int solve(vector<int>nums , int i , int Xor){\n if(i==nums.size()){\n return Xor;\n }\n int include = solve(nums , i+1 , nums[i]^Xor);\n int exclude = solve(nums , i+1 , Xor);\n\n return (include+exclude);\n }\n\n int subsetXORSum(vector<int>& nums) {\n return solve(nums , 0 , 0);\n }\n};", "memory": "30643" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n void recursiveHelper(vector<int> nums, int xorSum, int& finalSum, int index, int n)\n {\n if(index>=n)\n {\n finalSum+=xorSum;\n return;\n }\n\n xorSum=xorSum^nums[index];\n recursiveHelper(nums, xorSum, finalSum, index+1, n);\n\n xorSum=xorSum^nums[index];\n recursiveHelper(nums, xorSum, finalSum, index+1, n);\n }\n\n\n\n int subsetXORSum(vector<int>& nums) {\n\n int xorSum=0;\n int finalSum=0;\n int n=nums.size();\n\n recursiveHelper(nums, xorSum, finalSum, 0, n);\n\n return finalSum;\n \n }\n};", "memory": "30643" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void subset(int idx,vector<int> & ds,vector<int>nums,int n,vector<int>&ans){\n if(idx==n){\n int sum=0;\n for(auto it:ds){\n sum^=it;\n }\n ans.push_back(sum);\n return;\n }\n\n subset(idx+1,ds,nums,n,ans);\n\n ds.push_back(nums[idx]);\n subset(idx+1,ds,nums,n,ans);\n ds.pop_back();\n }\n int subsetXORSum(vector<int>& nums) {\n vector<int>ds;\n vector<int>ans;\n int n = nums.size();\n subset(0,ds,nums,n,ans);\n int x=0;\n for(auto it:ans){\n x+=it;\n }\n\n\n return x; \n }\n};", "memory": "31071" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void solve(vector<int>nums,vector<int>&xors,int start,vector<int>&ans){\n if(start == nums.size()){\n int s = 0;\n for(int i=0;i<ans.size();i++){\n s = s^ans[i];\n }\n xors.push_back(s);\n return;\n }\n //include;\n solve(nums,xors,start+1,ans);\n ans.push_back(nums[start]);\n solve(nums,xors,start+1,ans);\n\n ans.pop_back();\n return;\n }\n int subsetXORSum(vector<int>& nums) {\n if(nums.size() == 0)\n {\n return 0;\n }\n vector<int>xors;\n int start =0;\n vector<int>ans;\n solve(nums,xors,start,ans); \n int sum =0;\n for(int i=0;i<xors.size();i++)\n {\n sum = sum + xors[i];\n } \n\n return sum;\n }\n};", "memory": "31498" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\n vector<int> res;\n vector<int> curr;\npublic:\n int calcXORsum(vector<int>&arr){\n int sum = 0;\n for(int i=0; i<arr.size(); i++){\n sum ^= arr[i];\n }\n return sum;\n }\n\n int subsetXORSum(vector<int>& nums) {\n int tot = 0;\n\n dfs(0, nums.size(), curr, nums);\n\n for(int i=0; i<res.size(); i++){\n tot += res[i];\n } \n\n return tot;\n \n }\n\n void dfs(int i, int n, vector<int>& curr, vector<int> nums){\n if(i >= n){\n res.push_back(calcXORsum(curr));\n return;\n }\n\n curr.push_back(nums[i]);\n dfs(i+1, n, curr, nums);\n curr.pop_back();\n dfs(i+1, n, curr, nums);\n return;\n }\n\n\n};", "memory": "31498" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n vector<vector<int>>result={{}};\n int sum=0;\n \n for(auto num:nums)\n {\n int n=result.size();\n for(int i=0;i<n;i++)\n {\n vector<int>subset=result[i];\n subset.push_back(num);\n result.push_back(subset);\n int current=0;\n for(auto x:subset)\n {\n current^=x;\n }\n sum+=current;\n\n }\n\n }\n return sum;\n \n }\n};", "memory": "31926" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n vector<vector<int>> subsets(1);\n for(int num : nums) {\n size_t subsets_size = subsets.size();\n for(size_t i = 0; i < subsets_size; ++i) {\n vector<int> tmp = subsets[i];\n tmp.push_back(num);\n subsets.push_back(tmp);\n }\n }\n int result = 0;\n for(const auto& subset : subsets) {\n int intermediate = 0;\n for(int num : subset) {\n intermediate ^= num;\n }\n result += intermediate;\n }\n return result;\n }\n};", "memory": "31926" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "void createSubsets(int index, std::vector<int> subset, std::vector<std::vector<int>>& subsets, const std::vector<int>& nums)\n{\n\tsubset.push_back(nums[index]);\n\tsubsets.push_back(subset);\n\tfor (int i = index + 1; i < nums.size(); i++)\n\t{\n\t\tcreateSubsets(i, subset, subsets, nums);\n\t}\n}\n\nclass Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n std::vector<std::vector<int>> subsets;\n for (int i = 0; i < nums.size(); i++)\n {\n createSubsets(i, {}, subsets, nums);\n }\n\n int sum = 0;\n for (const std::vector<int>& subset : subsets)\n {\n int xorValue = 0;\n for (const int value : subset)\n {\n std::cout << value << \"-\";\n xorValue ^= value;\n }\n std::cout << std::endl;\n sum += xorValue;\n } \n\n return sum;\n }\n};", "memory": "32353" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> result;\n\n void solve(vector<int>& nums, int i, vector<int>& temp) {\n if(i >= nums.size()) {\n result.push_back(temp);\n return;\n }\n\n temp.push_back(nums[i]);\n solve(nums, i+1, temp);\n temp.pop_back();\n solve(nums, i+1, temp);\n }\n\n vector<vector<int>> subsets(vector<int>& nums) {\n vector<int> temp;\n\n solve(nums, 0, temp);\n\n return result;\n }\n\n int subsetXORSum(vector<int>& nums) {\n subsets(nums);\n int sum=0;\n for(auto i : result){\n int ans =0;\n for(auto j : i) {\n ans = ans^j;\n }\n sum += ans;\n\n }\n return sum;\n }\n};\n", "memory": "32781" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n void dfs(int index, int n, vector<int> nums, vector<vector<int>> &v, vector<int> &aux){\n v.push_back(aux);\n\n for(int i=index;i<n;i++){\n aux.push_back(nums[i]);\n dfs(i+1,n,nums,v,aux);\n aux.pop_back();\n }\n\n \n }\n\n int subsetXORSum(vector<int>& nums) {\n\n int n = nums.size();\n\n vector<vector<int>> res;\n vector<int> aux;\n dfs(0,n,nums,res, aux);\n int Xor = 0;\n for(auto v: res ){\n int ans=0;\n for(int i:v){\n ans=ans^i;\n }\n Xor+=ans;\n }\n\n return Xor;\n \n }\n};", "memory": "33208" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int XORcount(vector<int> subset) {\n int XOR = subset[0];\n for (int i = 1; i < subset.size(); i++)\n XOR = XOR ^ subset[i];\n return XOR;\n }\n void SubsetCount(vector<int> nums, vector<int>& subset,\n vector<vector<int>>& subsetVault, int index) {\n subsetVault.push_back(subset);\n\n for (int i = index; i < nums.size(); i++) {\n subset.push_back(nums[i]);\n SubsetCount(nums, subset, subsetVault, i + 1);\n subset.pop_back();\n }\n }\n int subsetXORSum(vector<int>& nums) {\n int n = nums.size();\n vector<int> subset;\n vector<vector<int>> subsetVault;\n int index = 0;\n SubsetCount(nums, subset, subsetVault, index);\n int sum = 0;\n\n for (int i = 1; i < subsetVault.size(); i++)\n sum += XORcount(subsetVault[i]);\n\n return sum;\n }\n};", "memory": "33636" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int XORcount(vector<int> subset) {\n int XOR = subset[0];\n for (int i = 1; i < subset.size(); i++)\n XOR = XOR ^ subset[i];\n return XOR;\n }\n void SubsetCount(vector<int> nums, vector<int>& subset,\n vector<vector<int>>& subsetVault, int index) {\n subsetVault.push_back(subset);\n\n for (int i = index; i < nums.size(); i++) {\n subset.push_back(nums[i]);\n SubsetCount(nums, subset, subsetVault, i + 1);\n subset.pop_back();\n }\n }\n int subsetXORSum(vector<int>& nums) {\n int n = nums.size();\n vector<int> subset;\n vector<vector<int>> subsetVault;\n int index = 0;\n SubsetCount(nums, subset, subsetVault, index);\n int sum = 0;\n\n for (int i = 1; i < subsetVault.size(); i++)\n sum += XORcount(subsetVault[i]);\n\n return sum;\n }\n};", "memory": "33636" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n int n = nums.size();\n int ans=0;\n f(nums,{}, 0, ans);\n return ans;\n }\n\n void f(vector<int>& nums, vector<int> temp, int i, int &ans){\n if(i==nums.size()){\n int tempx = 0;\n for(auto x: temp) tempx^=x;\n ans+=tempx;\n return;\n }\n temp.push_back(nums[i]);\n f(nums, temp, i+1,ans);\n temp.pop_back();\n f(nums, temp, i+1,ans);\n \n }\n};", "memory": "34063" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int sum;\n \n\n void subsets(int pos, vector<int>& nums, vector<int> temp, int xorr){\n\n //base case\n if(pos >= nums.size()){\n sum += xorr;\n return;\n }\n\n //take\n temp.push_back(nums[pos]);\n \n subsets(pos+1, nums, temp, xorr^nums[pos]);\n temp.pop_back();\n subsets(pos+1, nums, temp, xorr);\n }\n\n int subsetXORSum(vector<int>& nums) {\n \n int n = nums.size();\n sum = 0;\n\n subsets(0, nums, {}, 0);\n\n return sum;\n }\n};", "memory": "34491" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\nvoid subsetsum(vector<int>&nums,vector<int>ref,int ind,int &sum){\n if(ind == nums.size()){\n int x = 0;\n for(int i = 0; i < ref.size(); i++){\n x^=ref[i];\n }\n sum += x;\n return;\n }\n subsetsum(nums,ref,ind+1,sum);\n ref.push_back(nums[ind]);\n subsetsum(nums,ref,ind+1,sum);\n}\n\n int subsetXORSum(vector<int>& nums) {\n int sum = 0;\n vector<int>ref;\n subsetsum(nums,ref,0,sum);\n return sum;\n }\n};", "memory": "34491" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int getXOR(vector<int>& subset) {\n if (subset.empty()) return 0;\n \n int res = subset[0];\n\n for (int i = 1; i < subset.size(); ++i) {\n res = res xor subset[i];\n }\n\n return res;\n }\n\n void backtrack(vector<int>& nums, int i, vector<int> subset, int& sum) {\n if (i == nums.size()) {\n sum += getXOR(subset);\n return;\n }\n\n backtrack(nums, i + 1, subset, sum);\n subset.push_back(nums[i]);\n backtrack(nums, i + 1, subset, sum);\n }\n\n int subsetXORSum(vector<int>& nums) {\n // 2 5 6\n // 010\n // 101\n // 110\n \n // 001 = 1\n // can only have one 1?\n // the sum of all xor totals for every subset of nums\n\n int res = 0;\n\n // we can do backtracking\n // but there must be an easier way\n backtrack(nums, 0, {}, res);\n return res;\n }\n};", "memory": "34918" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\nvoid subsetsum(vector<int>&nums,vector<int>ref,int ind,int &sum){\n if(ind == nums.size()){\n int x = 0;\n for(int i = 0; i < ref.size(); i++){\n x^=ref[i];\n }\n sum += x;\n return;\n }\n subsetsum(nums,ref,ind+1,sum);\n ref.push_back(nums[ind]);\n subsetsum(nums,ref,ind+1,sum);\n}\n\n int subsetXORSum(vector<int>& nums) {\n int sum = 0;\n vector<int>ref;\n subsetsum(nums,ref,0,sum);\n return sum;\n }\n};", "memory": "34918" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\n void f(int ind,int n,vector<int>temp , vector<int> &ans,vector<int> &nums){\n if(ind==n){\n int xor_arr= 0;\n for(int i=0;i<temp.size();i++){\n xor_arr = xor_arr^temp[i];\n }\n ans.push_back(xor_arr);\n return ;\n }\n f(ind+1,n,temp,ans,nums);\n temp.push_back(nums[ind]);\n f(ind+1,n,temp,ans,nums);\n\n }\npublic:\n int subsetXORSum(vector<int>& nums) {\n int n = nums.size();\n vector<int> ans;\n vector<int> temp;\n f(0,n,temp,ans,nums);\n int sum=0;\n for(int i=0;i<ans.size();i++){\n sum+=ans[i];\n }\n return sum;\n }\n};", "memory": "35346" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n void solve(vector<int>& nums, int index, vector<int> output, vector<int>& ans){\n // base case\n if(index >= nums.size()){\n int x = 0;\n for(int i = 0; i < output.size(); i++){\n x = x ^ output[i];\n }\n ans.push_back(x);\n return;\n }\n //exclude\n solve(nums,index+1,output,ans);\n //include\n output.push_back(nums[index]);\n solve(nums,index+1,output,ans);\n }\npublic:\n int subsetXORSum(vector<int>& nums) {\n vector<int> ans;\n vector<int> output;\n\n int index = 0;\n solve(nums, index, output, ans);\n int sum = 0;\n for(int i = 0; i < ans.size(); i++){\n sum += ans[i];\n }\n return sum;\n }\n};", "memory": "35773" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void subsetXORSumRecursion(vector<int>& nums,int index,vector<int> curr,vector<int>& xorSum){\n //Base Case\n if(index>=nums.size()){\n int xor_value = 0;\n if(curr.size()!=0){\n \n for(auto a: curr){\n xor_value=(xor_value^a);\n //cout<<xor_value<<endl;\n //cout<<a<<\" \";\n\n }\n // cout<<endl;\n // cout<<xor_value;\n xorSum.push_back(xor_value);\n return;\n }\n return;\n \n }\n\n //include Case\n curr.push_back(nums[index]);\n subsetXORSumRecursion(nums,index+1,curr,xorSum);\n curr.pop_back();\n\n subsetXORSumRecursion(nums,index+1,curr,xorSum);\n }\n int subsetXORSum(vector<int>& nums) {\n vector<int> xorSum;\n subsetXORSumRecursion(nums, 0, {}, xorSum);\n int sum = 0;\n for(auto a: xorSum){\n sum = sum +a;\n }\n return sum;\n }\n};", "memory": "35773" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n int n = nums.size();\n int subsets = 1<<n;\n vector<vector<int>> powerset;\n\n for(int i = 0 ; i < subsets; i++)\n {\n vector<int> someset;\n for(int k = 0; k < n ; k++)\n {\n if(i&(1<<k))\n {\n someset.push_back(nums[k]);\n }\n } \n powerset.push_back(someset);\n }\n int sum = 0;\n for(int i = 0; i < subsets; i++)\n {\n int p = 0;\n if(powerset[i].size() > 0)\n {\n for(int k = 0; k < powerset[i].size();k++)\n {\n p = p^powerset[i][k];\n }\n }\n sum += p; \n }\n return sum;\n \n }\n};", "memory": "36201" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n vector<vector<int>> result;\n for (int i = 0; i < pow(2, nums.size()); i++) {\n vector<int> v;\n result.push_back(v);\n }\n int x = pow(2, nums.size()) / 2;\n for (int i : nums) {\n int j = 0;\n int cnt = 0;\n bool put = true;\n while (j < result.size()) {\n if (put) {\n result[j].push_back(i);\n }\n j++;\n cnt++;\n if (cnt == x) {\n put = !put;\n cnt = 0;\n }\n }\n x /= 2;\n }\n int sum = 0;\n for (vector<int> v : result) {\n int gay = 0;\n for (int i : v) {\n gay ^= i;\n }\n sum += gay;\n }\n return sum;\n }\n};", "memory": "36628" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n vector<vector<int>> temp;\n int size = nums.size();\n for (int i = 0; i < (1 << size); i++) {\n vector<int> subset;\n for (int j = 0; j < size; j++) {\n if (i & (1 << j)) {\n subset.push_back(nums[j]);\n }\n }\n temp.push_back(subset);\n }\n int ans = 0;\n for(int i=0;i<temp.size();i++){\n int subtemp=0;\n for(int j=0;j<temp[i].size();j++){\n subtemp^=temp[i][j];\n }\n ans+=subtemp;\n }\n \n return ans;\n }\n};", "memory": "37056" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& arr) {\n int totalSum = 0; // To hold the sum of XOR values of all subsets\n vector<vector<int>> subsets = {{}}; // Start with the empty subset\n\n // Iterate over each element in the array\n for (int i = 0; i < arr.size(); ++i) {\n int n = subsets.size();\n // For each subset, create a new subset by adding the current element\n for (int j = 0; j < n; ++j) {\n vector<int> newSubset = subsets[j]; // Copy the existing subset\n newSubset.push_back(arr[i]); // Add the current element\n subsets.push_back(newSubset); // Add the new subset to the list\n }\n }\n\n // Calculate XOR total for each subset and sum it up\n for (auto subset : subsets) {\n int xorTotal = 0;\n for (int x : subset) {\n xorTotal ^= x; // Calculate XOR of the current subset\n }\n totalSum += xorTotal; // Add XOR total of this subset to the overall sum\n }\n\n return totalSum; // Return the total sum of XOR values for all subsets\n }\n};", "memory": "37056" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n if(nums.size() == 0)\n return 0;\n int n = nums.size();\n vector<vector<int>>v;\n for (int i = 0; i < (1 << n); i++) {\n vector<int>temp;\n for (int j = 0; j < n; j++) {\n if ((i & (1 << j)) != 0) {\n temp.push_back(nums[j]);\n }\n }\n v.push_back(temp);\n }\n vector<int>s;\n for(int i=0;i<v.size();i++)\n {\n int t = 0;\n for(int j=0;j<v[i].size();j++)\n t = t ^ v[i][j];\n s.push_back(t);\n }\n int ans = 0;\n for(int i=0;i<s.size();i++)\n ans = ans + s[i];\n return ans;\n\n return ans;\n }\n};", "memory": "37483" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n void findSubsets(vector<vector<int>>& subsets, vector<int>& nums){\n int n = nums.size();\n int maxPossibilities = (1<< n);\n for(int i=0; i< maxPossibilities; i++){\n vector<int> list;\n for(int j=0; j< n; j++){\n //if the jth bit is set in binary rep of i\n if(i & (1<<j)) list.push_back(nums[j]);\n }\n subsets.push_back(list);\n }\n \n }\npublic:\n int subsetXORSum(vector<int>& nums) {\n //first task will be to find the subsets \n vector<vector<int>> subsets; \n findSubsets(subsets, nums);\n //now for each subset, find the XOR total\n int sum = 0;\n for(auto subset: subsets){\n if(subset.empty()) continue;\n int start = subset[0];\n for(int i=1; i< subset.size(); i++){\n start ^= subset[i]; \n }\n sum += start;\n }\n return sum;\n }\n};", "memory": "40476" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution\n{\npublic:\n int subsetXORSum(vector<int> &nums)\n {\n int n = nums.size();\n vector<vector<int>> subsets;\n\n for (int i = 0; i < (1 << n); ++i)\n {\n vector<int> subset;\n for (int j = 0; j < n; ++j)\n {\n if (i & (1 << j))\n {\n subset.push_back(nums[j]);\n }\n }\n subsets.push_back(subset);\n }\n\n int result = 0;\n for (auto subset : subsets)\n {\n int xorSum = 0;\n for (auto num : subset)\n {\n xorSum ^= num;\n }\n result += xorSum;\n }\n\n return result;\n }\n};", "memory": "40903" }
1,993
<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p> <ul> <li>For example, the <strong>XOR total</strong> of the array <code>[2,5,6]</code> is <code>2 XOR 5 XOR 6 = 1</code>.</li> </ul> <p>Given an array <code>nums</code>, return <em>the <strong>sum</strong> of all <strong>XOR totals</strong> for every <strong>subset</strong> of </em><code>nums</code>.&nbsp;</p> <p><strong>Note:</strong> Subsets with the <strong>same</strong> elements should be counted <strong>multiple</strong> times.</p> <p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3] <strong>Output:</strong> 6 <strong>Explanation: </strong>The 4 subsets of [1,3] are: - The empty subset has an XOR total of 0. - [1] has an XOR total of 1. - [3] has an XOR total of 3. - [1,3] has an XOR total of 1 XOR 3 = 2. 0 + 1 + 3 + 2 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,1,6] <strong>Output:</strong> 28 <strong>Explanation: </strong>The 8 subsets of [5,1,6] are: - The empty subset has an XOR total of 0. - [5] has an XOR total of 5. - [1] has an XOR total of 1. - [6] has an XOR total of 6. - [5,1] has an XOR total of 5 XOR 1 = 4. - [5,6] has an XOR total of 5 XOR 6 = 3. - [1,6] has an XOR total of 1 XOR 6 = 7. - [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2. 0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,5,6,7,8] <strong>Output:</strong> 480 <strong>Explanation:</strong> The sum of all XOR totals for every subset is 480. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 12</code></li> <li><code>1 &lt;= nums[i] &lt;= 20</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int subsetXORSum(vector<int>& nums) {\n int n = nums.size();\n int subsetcount = 1<<n;\n vector<vector<int>> allsubsets;\n for(int i=0; i<subsetcount; i++){\n vector<int> subset;\n for(int j=0; j<n; j++){\n if((i & (1<<j)) != 0){\n subset.push_back(nums[j]);\n }\n }\n allsubsets.push_back(subset);\n }\n int sum = 0;\n for(auto sets: allsubsets){\n int x = 0;\n if(sets.size()!=0){\n for(auto set: sets){\n x^= set;\n }\n sum += x;\n }\n }\n return sum;\n }\n};", "memory": "41331" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "using P2I = pair<int, int>;\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n int parent[n];\n int count[n];\n for(int i=0; i<n; i++){\n parent[i] = -1;\n count[i] = 1;\n }\n\n for(int i=0; i<edges.size(); i++){\n int x = edges[i][0];\n int y = edges[i][1];\n edges[i].push_back(max(vals[x], vals[y]));\n }\n sort(edges.begin(), edges.end(), [](const vector<int>& a, const vector<int>& b){\n return a[2]<b[2];\n });\n int cnt = 0;\n for(auto& edge: edges){\n int x = edge[0];\n int y = edge[1];\n int px = __find(parent, x);\n int py = __find(parent, y);\n if(vals[px]<vals[py]) parent[px] = py;\n else if(vals[px]>vals[py]) parent[py] = px;\n else{\n cnt += count[px]*count[py];\n count[px] += count[py];\n parent[py] = px;\n }\n }\n return cnt+n;\n }\n\n int __find(int* parent, int x){\n if(parent[x]==-1) return x;\n return parent[x] = __find(parent, parent[x]);\n }\n};", "memory": "140327" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "using P2I = pair<int, int>;\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n int parent[n];\n int count[n];\n for(int i=0; i<n; i++){\n parent[i] = -1;\n count[i] = 1;\n }\n\n for(int i=0; i<edges.size(); i++){\n int x = edges[i][0];\n int y = edges[i][1];\n edges[i].push_back(max(vals[x], vals[y]));\n }\n sort(edges.begin(), edges.end(), [](const vector<int>& a, const vector<int>& b){\n return a[2]<b[2];\n });\n int cnt = 0;\n for(auto& edge: edges){\n int x = edge[0];\n int y = edge[1];\n int px = __find(parent, x);\n int py = __find(parent, y);\n if(vals[px]<vals[py]) parent[px] = py;\n else if(vals[px]>vals[py]) parent[py] = px;\n else{\n cnt += count[px]*count[py];\n count[px] += count[py];\n parent[py] = px;\n }\n }\n return cnt+n;\n }\n\n int __find(int* parent, int x){\n if(parent[x]==-1) return x;\n return parent[x] = __find(parent, parent[x]);\n }\n};", "memory": "142260" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int par[30007];\n int mx[30007];\n int frq[30007];\n \n int find(int u){\n if(u==par[u]) return u;\n return par[u]=find(par[u]);\n }\n \n /*\n Hard Problem :(\n */\n \n int numberOfGoodPaths(vector<int>& v, vector<vector<int>>& e) {\n int n=v.size();\n for(int i=0;i<n;i++){\n par[i]=i;\n mx[i]=v[i];\n frq[i]=1;\n }\n \n for(auto &i:e){\n i.push_back(max(v[i[0]],v[i[1]]));\n }\n \n sort(e.begin(),e.end(),[](auto &v1, auto &v2){\n return v1[2] < v2[2];\n });\n \n int ans=n;\n for(auto &i:e){\n int par1=find(i[0]),par2=find(i[1]);\n int mx1=mx[par1],mx2=mx[par2];\n int f1=frq[par1],f2=frq[par2];\n \n if(mx1==mx2){\n ans+=f1*f2;\n if(f1<f2){\n par[par1]=par2;\n frq[par2]+=frq[par1];\n }\n else{\n par[par2]=par1;\n frq[par1]+=frq[par2];\n }\n }\n else if(mx1>mx2){\n par[par2]=par1;\n }\n else{\n par[par1]=par2;\n }\n }\n return ans;\n \n \n }\n};", "memory": "142260" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class DSU {\npublic:\n vector<int> parent;\n vector<pair<int, int>>m; //{maxVal, count of maxVal}\n DSU(vector<int> &vals) : parent(vals.size()), m(vals.size()) {\n for(int i = 0; i < vals.size(); i++) {\n parent[i] = i;\n m[i] = {vals[i], 1};\n }\n }\n\n int Find(int x) {\n if(x != parent[x]) {\n parent[x] = Find(parent[x]);\n }\n return parent[x];\n }\n\n int Union(int x, int y, int maxVal) {\n int xp = Find(x);\n int yp = Find(y);\n if(xp == yp) {\n return 0;\n }\n int cx = m[xp].first == maxVal ? m[xp].second : 0;\n int cy = m[yp].first == maxVal ? m[yp].second : 0;\n\n parent[yp] = xp;\n m[xp] = {maxVal, cx + cy};\n return cx * cy;\n }\n\n};\n\nclass Solution {\npublic:\n \n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n for(auto &e : edges) {\n e.push_back(max(vals[e[0]], vals[e[1]]));\n }\n sort(edges.begin(), edges.end(), [&](vector<int> &a, vector<int> &b) {\n return a[2] < b[2];\n });\n\n DSU dsu(vals);\n\n int ans = n;\n\n for(auto &e : edges) {\n ans += dsu.Union(e[0], e[1], e[2]);\n }\n\n return ans;\n }\n};\n// Hint 1\n// Can you process nodes from smallest to largest value?\n// Hint 2\n// Try to build the graph from nodes with the smallest value to the largest value.\n// Hint 3\n// May union find help?", "memory": "144192" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class DSU {\npublic:\n vector<int> parent;\n vector<pair<int, int>>m; //{maxVal, count of maxVal}\n DSU(vector<int> &vals) : parent(vals.size()), m(vals.size()) {\n for(int i = 0; i < vals.size(); i++) {\n parent[i] = i;\n m[i] = {vals[i], 1};\n }\n }\n\n int Find(int x) {\n if(x != parent[x]) {\n parent[x] = Find(parent[x]);\n }\n return parent[x];\n }\n\n int Union(int x, int y, int maxVal) {\n int xp = Find(x);\n int yp = Find(y);\n if(xp == yp) {\n return 0;\n }\n int cx = m[xp].first == maxVal ? m[xp].second : 0;\n int cy = m[yp].first == maxVal ? m[yp].second : 0;\n\n parent[yp] = xp;\n m[xp] = {maxVal, cx + cy};\n return cx * cy;\n }\n\n};\n\nclass Solution {\npublic:\n \n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n for(auto &e : edges) {\n e.push_back(max(vals[e[0]], vals[e[1]]));\n }\n sort(edges.begin(), edges.end(), [&](vector<int> &a, vector<int> &b) {\n return a[2] < b[2];\n });\n\n DSU dsu(vals);\n\n int ans = n;\n\n for(auto &e : edges) {\n ans += dsu.Union(e[0], e[1], e[2]);\n }\n\n return ans;\n }\n};\n// Hint 1\n// Can you process nodes from smallest to largest value?\n// Hint 2\n// Try to build the graph from nodes with the smallest value to the largest value.\n// Hint 3\n// May union find help?\n// Intuition\n// All single node are considered as a good path.\n// Take a look at the biggest value maxv\n// If the number of nodes with biggest value maxv is k,\n// then good path starting with value maxv is k*(k-1)/2,\n// any pair can build up a good path.\n// And these nodes will cut the tree into small trees.\n// Explanation\n// We do the \"cutting\" process reversely by union-find.\n\n// Firstly we initilize result res = n,\n// we union the edge with small values v.\n\n// Then the nodes of value v on one side,\n// can pair up with the nodes of value v on the other side.\n\n// We union one node into the other by union action,\n// and also the count of nodes with value v.\n\n// We reapeatly doing union action for all edges until we build up the whole tree.\n\n\n// Complexity\n// Time O(union-find(n))\n// Space O(union-find(n))", "memory": "146125" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n // dsu\n vector<int> root;\n int get(int x) {\n return x == root[x] ? x : (root[x] = get(root[x]));\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n // each node is a good path\n int n = vals.size(), ans = n;\n vector<int> cnt(n, 1);\n root.resize(n);\n // each element is in its own group initially\n for (int i = 0; i < n; i++) root[i] = i;\n // sort by vals\n sort(edges.begin(), edges.end(), [&](const vector<int>& x, const vector<int>& y) {\n return max(vals[x[0]], vals[x[1]]) < max(vals[y[0]], vals[y[1]]);\n });\n // iterate each edge\n for (auto e : edges) {\n int x = e[0], y = e[1];\n // get the root of x\n x = get(x);\n // get the root of y\n y = get(y);\n // if their vals are same, \n if (vals[x] == vals[y]) {\n // then there would be cnt[x] * cnt[y] good paths\n ans += cnt[x] * cnt[y];\n // unite them\n root[x] = y;\n // add the count of x to that of y\n cnt[y] += cnt[x];\n } else if (vals[x] > vals[y]) {\n // unite them\n root[y] = x;\n } else {\n // unite them\n root[x] = y;\n }\n }\n return ans;\n }\n};\n\n// [3,2]\n// [1,2,3]\n\n// 3 - 1 - 2 - 3\n// 3 - 2 - 3\n// 3 - 2 - 1 - 3\n// 3 - 2 - 2 - 3\n// 3 - 2 - 1 - 2 - 3\n// 3 - 3\n// good paths += cnt[x] * cnt[y]", "memory": "146125" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class DisjointSet{\n public:\n vector<int>parent,ranks;\n\n DisjointSet(int n){\n for(int i=0;i<=n;i++){\n parent.push_back(i);\n ranks.push_back(1);\n }\n }\n int getUltParent(int node){\n if(node==parent[node])return node;\n return parent[node]=getUltParent(parent[node]);\n }\n};\nstatic vector<int> temp;\nclass Solution {\n int ds_union(DisjointSet &ds,int u,int v,vector<int>&vals){\n int upu=ds.getUltParent(u);\n int upv=ds.getUltParent(v);\n int ans=0;\n if(vals[upu]==vals[upv]){\n ans+=ds.ranks[upu]*ds.ranks[upv];\n ds.parent[upu]=upv;\n ds.ranks[upv]+=ds.ranks[upu];\n }\n else if(vals[upu]<vals[upv]){\n ds.parent[upu]=upv;\n }\n else{\n ds.parent[upv]=upu;\n }\n\n return ans;\n }\npublic:\n bool static comp(vector<int>&a,vector<int>&b){\n return max(temp[a[0]],temp[a[1]])<max(temp[b[0]],temp[b[1]]);\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n=vals.size();\n temp=vals;\n sort(edges.begin(),edges.end(),comp);\n int ans=n;\n DisjointSet ds(n);\n for(auto edge:edges){\n ans+=ds_union(ds,edge[0],edge[1],vals);\n }\n return ans;\n }\n};", "memory": "148057" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class DisjointSet{\n public:\n vector<int>parent,ranks;\n\n DisjointSet(int n){\n for(int i=0;i<=n;i++){\n parent.push_back(i);\n ranks.push_back(1);\n }\n }\n int getUltParent(int node){\n if(node==parent[node])return node;\n return parent[node]=getUltParent(parent[node]);\n }\n};\nstatic vector<int> temp;\nclass Solution {\n int ds_union(DisjointSet &ds,int u,int v,vector<int>&vals){\n int upu=ds.getUltParent(u);\n int upv=ds.getUltParent(v);\n int ans=0;\n if(vals[upu]==vals[upv]){\n ans+=ds.ranks[upu]*ds.ranks[upv];\n ds.parent[upu]=upv;\n ds.ranks[upv]+=ds.ranks[upu];\n }\n else if(vals[upu]<vals[upv]){\n ds.parent[upu]=upv;\n }\n else{\n ds.parent[upv]=upu;\n }\n\n return ans;\n }\npublic:\n bool static comp(vector<int>&a,vector<int>&b){\n return max(temp[a[0]],temp[a[1]])<max(temp[b[0]],temp[b[1]]);\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n=vals.size();\n temp=vals;\n sort(edges.begin(),edges.end(),comp);\n int ans=n;\n DisjointSet ds(n);\n for(auto edge:edges){\n ans+=ds_union(ds,edge[0],edge[1],vals);\n }\n return ans;\n }\n};", "memory": "149990" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": " class Solution {\npublic:\n struct UnionFind {\n UnionFind(int n) : parent(n, 0) {\n for (int i = 0; i < n; i++) {\n parent[i] = i;\n }\n }\n int Find(int x) {\n int temp = x;\n while (parent[temp] != temp) {\n temp = parent[temp];\n }\n while (x != parent[temp]) {\n int next = parent[x];\n parent[x] = temp;\n x = next;\n }\n return temp;\n }\n void Union(int x, int y, vector<int>& vals) {\n int xx = Find(x), yy = Find(y);\n if (xx != yy) {\n if (vals[xx] < vals[yy]) {\n parent[xx] = yy;\n } else {\n parent[yy] = xx;\n }\n }\n }\n private:\n std::vector<int> parent; \n };\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n UnionFind uf(vals.size());\n unordered_map<int, int> freq;\n for (int i = 0; i < vals.size(); i++) {\n freq[i] = 1;\n }\n sort(edges.begin(), edges.end(), [&](const auto& lhs, const auto& rhs){\n return max(vals[lhs[0]], vals[lhs[1]]) < max(vals[rhs[0]], vals[rhs[1]]);\n });\n int res = vals.size();\n for (auto& e : edges) {\n auto x = uf.Find(e[0]);\n auto y = uf.Find(e[1]);\n if (vals[x] == vals[y]) {\n res += freq[x] * freq[y];\n freq[x] += freq[y];\n }\n uf.Union(x, y, vals);\n }\n return res;\n }\n};", "memory": "149990" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n struct UnionFind {\n UnionFind(int n) : parent(n, 0) {\n for (int i = 0; i < n; i++) {\n parent[i] = i;\n }\n }\n int Find(int x) {\n int temp = x;\n while (parent[temp] != temp) {\n temp = parent[temp];\n }\n while (x != parent[temp]) {\n int next = parent[x];\n parent[x] = temp;\n x = next;\n }\n return temp;\n }\n void Union(int x, int y, vector<int>& vals) {\n int xx = Find(x), yy = Find(y);\n if (xx != yy) {\n if (vals[xx] < vals[yy]) {\n parent[xx] = yy;\n } else {\n parent[yy] = xx;\n }\n }\n }\n private:\n std::vector<int> parent; \n };\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n UnionFind uf(vals.size());\n unordered_map<int, int> freq;\n for (int i = 0; i < vals.size(); i++) {\n freq[i] = 1;\n }\n sort(edges.begin(), edges.end(), [&](const auto& lhs, const auto& rhs){\n return max(vals[lhs[0]], vals[lhs[1]]) < max(vals[rhs[0]], vals[rhs[1]]);\n });\n int res = vals.size();\n for (auto& e : edges) {\n auto x = uf.Find(e[0]);\n auto y = uf.Find(e[1]);\n if (vals[x] == vals[y]) {\n res += freq[x] * freq[y];\n freq[x] += freq[y];\n }\n uf.Union(x, y, vals);\n }\n return res;\n }\n};", "memory": "151922" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n\tint find(vector<int>& root,int i) {\n\t\t//if(i==root[i]) return i;\n\t\t//root[i]=find(root,root[i]);\n\t\twhile(root[i] != i){\n root[i] = root[root[i]];\n i = root[i];\n }\n return i;\n\t}\n\tint numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size(),m=edges.size(),ans=0;\n\n // X 是根权向量 x[i][0]表示i的val, x[i][1]表示i作为根的根权\n\t\tvector<vector<int>> x(n);\n\t\tvector<int> root(n);\n\t\tfor(int i=0;i<n;i++){\n\t\t\troot[i]=i;\n\t\t\tx[i]={vals[i],1};\n\t\t}\n //for(auto & e : edges){\n // cout << vals[e[0]] << \"<->\" << vals[e[1]] << \",\";\n //}\n //cout << endl;\n // 边的排序很重要, val小的放在前面, 大的放后面\n sort(edges.begin(),edges.end(),[&](vector<int>& a,vector<int>& b){\n\t \treturn max(vals[a[0]],vals[a[1]])<max(vals[b[0]],vals[b[1]]);\n\t\t});\n //for(auto & e : edges){\n // cout << vals[e[0]] << \"<->\" << vals[e[1]] << \",\";\n //}\n\t\tfor(int i=0;i<m;i++){\n\t\t\tint a=find(root,edges[i][0]);\n\t\t\tint b=find(root,edges[i][1]);\n //cout << \"a = \" << a << \" ,b= \" << b << endl;\n\t\t\tif(x[a][0]!=x[b][0]){\n // 一条边的两端的根不等值时,value大的根成为value小根的根, 即大根为根merge, 不计算根权\n\t\t\t\tif(x[a][0]>x[b][0]) root[b]=a;\n\t\t\t\telse root[a]=b;\n\t\t\t}\n\t\t\telse{\n // 如果两根等值, merge的同时,根权相乘后是答案的增量\n // b 作为a的根, b的根权吸收a的根权\n // 根权是正值, 初始为1\n\t\t\t\troot[a]=b;\n\t\t\t\tans+=x[a][1]*x[b][1];\n\t\t\t\tx[b][1]+=x[a][1];\n\t\t\t}\n\t\t}\n\t\treturn ans+n;\n\t}\n};", "memory": "151922" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n\tint find(vector<int>& y,int i) {\n\t\tif(i==y[i]) return i;\n\t\ty[i]=find(y,y[i]);\n\t\treturn y[i];\n\t}\n\tint numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size(),m=edges.size(),ans=0;\n\t\tvector<vector<int>> x(n);\n\t\tvector<int> y(n);\n\t\tfor(int i=0;i<n;i++){\n\t\t\ty[i]=i;\n\t\t\tx[i]={vals[i],1};\n\t\t}\n sort(edges.begin(),edges.end(),[&](vector<int>& a,vector<int>& b){\n\t \treturn max(vals[a[0]],vals[a[1]])<max(vals[b[0]],vals[b[1]]);\n\t\t});\n\t\tfor(int i=0;i<m;i++){\n\t\t\tint a=find(y,edges[i][0]);\n\t\t\tint b=find(y,edges[i][1]);\n\t\t\tif(x[a][0]!=x[b][0]){\n\t\t\t\tif(x[a][0]>x[b][0]) y[b]=a;\n\t\t\t\telse y[a]=b;\n\t\t\t}\n\t\t\telse{\n\t\t\t\ty[a]=b;\n\t\t\t\tans+=x[a][1]*x[b][1];\n\t\t\t\tx[b][1]+=x[a][1];\n\t\t\t}\n\t\t}\n\t\treturn ans+n;\n\t}\n};", "memory": "153855" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n\tint find(vector<int>& y,int i) {\n\t\tif(i==y[i]) return i;\n\t\ty[i]=find(y,y[i]);\n\t\treturn y[i];\n\t}\n\tint numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size(),m=edges.size(),ans=0;\n\t\tvector<vector<int>> x(n);\n\t\tvector<int> y(n);\n\t\tfor(int i=0;i<n;i++){\n\t\t\ty[i]=i;\n\t\t\tx[i]={vals[i],1};\n\t\t}\n sort(edges.begin(),edges.end(),[&](vector<int>& a,vector<int>& b){\n\t \treturn max(vals[a[0]],vals[a[1]])<max(vals[b[0]],vals[b[1]]);\n\t\t});\n\t\tfor(int i=0;i<m;i++){\n\t\t\tint a=find(y,edges[i][0]);\n\t\t\tint b=find(y,edges[i][1]);\n\t\t\tif(x[a][0]!=x[b][0]){\n\t\t\t\tif(x[a][0]>x[b][0]) y[b]=a;\n\t\t\t\telse y[a]=b;\n\t\t\t}\n\t\t\telse{\n\t\t\t\ty[a]=b;\n\t\t\t\tans+=x[a][1]*x[b][1];\n\t\t\t\tx[b][1]+=x[a][1];\n\t\t\t}\n\t\t}\n\t\treturn ans+n;\n\t}\n};", "memory": "153855" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n inline int\n numberOfGoodPaths(std::vector<int>& vals,\n const std::vector<std::vector<int>>& edges) noexcept {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n vals_ = std::move(vals);\n const auto n = static_cast<int>(vals_.size());\n std::vector<std::vector<int>> adjacency_list(n);\n for (const std::vector<int>& edge : edges) {\n if (vals_[edge[0]] > vals_[edge[1]]) {\n adjacency_list[edge[0]].emplace_back(edge[1]);\n } else {\n adjacency_list[edge[1]].emplace_back(edge[0]);\n }\n }\n parents_.resize(n);\n std::iota(parents_.begin(), parents_.end(), 0);\n std::vector<int> indices_by_vals = parents_;\n std::sort(\n indices_by_vals.begin(), indices_by_vals.end(),\n [&](int lhs, int rhs) noexcept { return vals_[lhs] < vals_[rhs]; });\n frequencies_.assign(n, 1);\n ret_ = n;\n for (const int u : indices_by_vals) {\n for (const int v : adjacency_list[u]) {\n UnionSets(u, v);\n }\n }\n return ret_;\n }\n\nprivate:\n inline int FindRepresentative(int v) noexcept {\n if (parents_[v] == v) {\n return v;\n }\n parents_[v] = FindRepresentative(parents_[v]);\n return parents_[v];\n }\n\n inline void UnionSets(int u, int v) noexcept {\n u = FindRepresentative(u);\n v = FindRepresentative(v);\n if (u != v) {\n if (vals_[u] == vals_[v]) {\n ret_ += frequencies_[u] * frequencies_[v];\n if (frequencies_[u] < frequencies_[v]) {\n parents_[u] = v;\n frequencies_[v] += frequencies_[u];\n } else {\n parents_[v] = u;\n frequencies_[u] += frequencies_[v];\n }\n } else if (vals_[u] < vals_[v]) {\n parents_[u] = v;\n } else {\n parents_[v] = u;\n }\n }\n }\n\n int ret_;\n std::vector<int> frequencies_;\n std::vector<int> vals_;\n std::vector<int> parents_;\n};\n", "memory": "155787" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n inline int\n numberOfGoodPaths(std::vector<int>& vals,\n const std::vector<std::vector<int>>& edges) noexcept {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n vals_ = std::move(vals);\n const auto n = static_cast<int>(vals_.size());\n std::vector<std::vector<int>> adjacency_list(n);\n for (const std::vector<int>& edge : edges) {\n if (vals_[edge[0]] > vals_[edge[1]]) {\n adjacency_list[edge[0]].emplace_back(edge[1]);\n } else {\n adjacency_list[edge[1]].emplace_back(edge[0]);\n }\n }\n parents_.resize(n);\n std::iota(parents_.begin(), parents_.end(), 0);\n std::vector<int> indices_by_vals = parents_;\n std::sort(indices_by_vals.begin(), indices_by_vals.end(),\n [&](int lhs, int rhs) { return vals_[lhs] < vals_[rhs]; });\n frequencies_.assign(n, 1);\n ret_ = n;\n for (const int u : indices_by_vals) {\n for (const int v : adjacency_list[u]) {\n UnionSets(u, v);\n }\n }\n return ret_;\n }\n\nprivate:\n inline int FindRepresentative(int v) noexcept {\n if (parents_[v] == v) {\n return v;\n }\n parents_[v] = FindRepresentative(parents_[v]);\n return parents_[v];\n }\n\n inline void UnionSets(int u, int v) noexcept {\n u = FindRepresentative(u);\n v = FindRepresentative(v);\n if (u != v) {\n if (vals_[u] == vals_[v]) {\n ret_ += frequencies_[u] * frequencies_[v];\n parents_[u] = v;\n frequencies_[v] += frequencies_[u];\n } else if (vals_[u] < vals_[v]) {\n parents_[u] = v;\n } else {\n parents_[v] = u;\n }\n }\n }\n\n int ret_;\n std::vector<int> frequencies_;\n std::vector<int> vals_;\n std::vector<int> parents_;\n};\n", "memory": "155787" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\nint par[100001], siize[100001];\nint find(int a) {\n\tif (par[a] == a) return a;\n\treturn par[a] = find(par[a]);\n}\n\n int numberOfGoodPaths(vector<int>& val, vector<vector<int>>& edges) {\n vector<vector<int>>v;\n int n=val.size();\n for(int i=0;i<val.size()-1;i++){\n v.push_back({max(val[edges[i][0]],val[edges[i][1]]),edges[i][0],edges[i][1]});\n }\n for(int i=0;i<n;i++){\n par[i]=i;\n siize[i]=1;\n }\n sort(v.begin(),v.end());\n int ans=0;\n for(int i=0;i<n-1;i++){\n int a=find(v[i][1]);\n int b=find(v[i][2]);\n if(val[a]<val[b]) swap(a,b);\n if(val[a]==val[b]){\n ans+=(siize[a]*siize[b]);\n par[b]=a;\n siize[a]+=siize[b];\n }\n else{\n par[b]=a;\n }\n }\n return ans+n;\n }\n};", "memory": "157720" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int comb(int i) {\n return i * (i - 1) / 2;\n }\n int find(vector<int>& parent, int cur) {\n if (parent[cur] == -1) return cur;\n int rs = find(parent, parent[cur]);\n parent[cur] = rs;\n return rs;\n }\n int unionf(vector<int>& parent, vector<int>& rank, int a, int b) {\n int ra = find(parent, a);\n int rb = find(parent, b);\n if (ra == rb) return ra;\n if (rank[ra] < rank[rb]) swap(ra, rb);\n parent[rb] = ra;\n rank[ra] = max(rank[ra], 1 + rank[rb]);\n return ra;\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n vector<int> parent (n, -1), rank (n);\n vector<pair<int, int>> cnt (n); // value, total\n vector<pair<int, int>> v;\n vector<vector<int>> adj (n);\n for (int i = 0; i < n; i++) {\n cnt[i] = {-1, 0};\n v.push_back({vals[i], i});\n }\n sort(v.begin(), v.end());\n for (auto &e : edges) {\n if (vals[e[0]] >= vals[e[1]]) adj[e[0]].push_back(e[1]);\n else adj[e[1]].push_back(e[0]);\n }\n int rs = n;\n for (int i = 0; i < n; i++) {\n int k = v[i].first;\n int cur = v[i].second;\n int tot = 0;\n int rc = find(parent, cur);\n if (cnt[rc].first == k) {\n tot += cnt[rc].second;\n rs -= comb(cnt[rc].second);\n }\n for (int nx : adj[cur]) {\n int rn = find(parent, nx);\n if (cnt[rn].first == k) {\n rs -= comb(cnt[rn].second);\n tot += cnt[rn].second;\n }\n unionf(parent, rank, cur, nx);\n }\n rc = find(parent, cur);\n cnt[rc] = {k, tot + 1};\n // cout << k << \" \" << cur << \" \" << cnt[rc].second << endl;\n rs += comb(cnt[rc].second);\n }\n return rs;\n }\n};", "memory": "159652" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n\n int g[30000];\n int f(int x) {\n return g[x] = (g[x] == x ? x : f(g[x]));\n }\n\n void uni(int x, int y) {\n int fx = f(x);\n int fy = f(y);\n g[fx] = fy;\n }\n\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n\n vector<int>ve;\n for(auto i : vals) ve.push_back(i);\n sort(ve.begin(), ve.end());\n ve.resize( unique(ve.begin(), ve.end() ) - ve.begin() );\n for(int i = 0; i < vals.size(); ++i) {\n vals[i] = lower_bound(ve.begin(), ve.end(), vals[i]) - ve.begin();\n }\n\n vector<int> valueG[30000];\n for(int i = 0; i < 30000; ++i) valueG[i].clear();\n for(int i = 0; i < vals.size(); ++i) valueG[vals[i]].push_back(i);\n\n vector<pair<int, int>>eList;\n for(int i = 0; i < edges.size(); ++i) {\n eList.push_back({max(vals[edges[i][0]], vals[edges[i][1]]), i});\n }\n sort(eList.begin(), eList.end());\n\n for(int i = 0; i < 30000; ++i) g[i] = i;\n\n int ans = 0, idx = 0;\n for(int i = 0; i < 30000; ++i) {\n map<int, int>ma;\n while(idx < eList.size() && eList[idx].first <= i) {\n uni(edges[eList[idx].second][0], edges[eList[idx].second][1]);\n ++idx;\n }\n ans += valueG[i].size();\n for(int j = 0; j < valueG[i].size(); ++j) ma[f(valueG[i][j])]++;\n for(auto j : ma) ans += j.second*(j.second-1)/2;\n }\n\n return ans;\n }\n};", "memory": "161585" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n const int n = vals.size();\n vector<vector<int>> tree(n);\n\n for (auto& e: edges) {\n tree[e[0]].push_back(e[1]);\n tree[e[1]].push_back(e[0]);\n }\n\n vector<int> uf(n);\n vector<int> count(n, 1);\n\n iota(uf.begin(), uf.end(), 0);\n\n vector<int> inds(n);\n iota(inds.begin(), inds.end(), 0);\n\n sort(inds.begin(), inds.end(), [&]( int a, int b) {\n return vals[a] < vals[b];\n });\n\n int ans = 0;\n for (int id: inds) {\n int p = getRoot(uf, id);\n\n for (int nei: tree[id]) {\n if (vals[nei] <= vals[id]) {\n nei = getRoot(uf, nei);\n merge(uf, p, nei);\n if (p != nei && vals[nei] == vals[id]) {\n ans += count[nei]*count[p];\n count[p] += count[nei];\n merge(uf, p, nei);\n }\n }\n }\n }\n\n return ans+n;\n }\n\nprivate:\n int getRoot(vector<int>& uf, int id) {\n if (uf[id] != id) {\n uf[id] = getRoot(uf, uf[id]);\n }\n\n return uf[id];\n }\n\n void merge(vector<int>& uf, int parent, int child) {\n child = getRoot(uf, child);\n\n if (parent != child) {\n uf[child] = parent;\n }\n }\n};", "memory": "161585" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n\tint find(vector<int>& parent,int i) {\n\t\tif(i == parent[i]) \n return i;\n\t\treturn parent[i]=find(parent ,parent[i]);\n\t}\n\tint numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n int ans = 0;\n\t\tvector<vector<int>> count(n);\n\t\tvector<int> parent(n);\n\t\tfor(int i=0;i<n;i++){\n\t\t\tparent[i]=i;\n\t\t\tcount[i]={vals[i],1};\n\t\t}\n sort(edges.begin(),edges.end(),[&](vector<int>& a,vector<int>& b){\n\t \treturn max(vals[a[0]],vals[a[1]])<max(vals[b[0]],vals[b[1]]);\n\t\t});\n\t\tfor(auto e: edges){\n\t\t\tint a=find(parent, e[0]);\n\t\t\tint b=find(parent, e[1]);\n\t\t\tif(count[a][0] != count[b][0]){\n //value of nodes is not equal\n\t\t\t\tif(count[a][0] > count[b][0]) \n parent[b]=a;\n\t\t\t\telse \n parent[a]=b;\n\t\t\t}\n\t\t\telse{\n\t\t\t\tparent[a] = b;\n\t\t\t\tans += count[a][1]*count[b][1];\n\t\t\t\tcount[b][1] += count[a][1];\n\t\t\t}\n\t\t}\n\t\treturn ans+n;\n\t}\n};", "memory": "163517" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "auto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n const int n = vals.size();\n vector<vector<int>> tree(n);\n\n for (auto& e: edges) {\n tree[e[0]].push_back(e[1]);\n tree[e[1]].push_back(e[0]);\n }\n\n vector<int> uf(n);\n vector<int> count(n, 1);\n\n iota(uf.begin(), uf.end(), 0);\n\n vector<int> inds(n);\n iota(inds.begin(), inds.end(), 0);\n\n sort(inds.begin(), inds.end(), [&]( int a, int b) {\n return vals[a] < vals[b];\n });\n\n int ans = 0;\n for (int id: inds) {\n int p = getRoot(uf, id);\n\n for (int nei: tree[id]) {\n if (vals[nei] <= vals[id]) {\n nei = getRoot(uf, nei);\n merge(uf, p, nei);\n if (p != nei && vals[nei] == vals[id]) {\n ans += count[nei]*count[p];\n count[p] += count[nei];\n\n /* if (count[p] >= count[nei]) {\n merge(uf, p, nei);\n } else {\n merge(uf, nei, p);\n }*/\n }\n }\n }\n }\n\n return ans+n;\n }\n\nprivate:\n int getRoot(vector<int>& uf, int id) {\n if (uf[id] != id) {\n uf[id] = getRoot(uf, uf[id]);\n }\n\n return uf[id];\n }\n\n void merge(vector<int>& uf, int parent, int child) {\n child = getRoot(uf, child);\n\n if (parent != child) {\n uf[child] = parent;\n }\n }\n};", "memory": "165450" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\n#ifdef LC_LOCAL\n#include \"parser.hpp\"\n#else\n#define dbg(...)\n#endif\n\n// ----- CHANGE FOR PROBLEM -----\ntemplate <invocable<int, int> F>\nstruct union_find {\n vector<int> par, size;\n F f;\n union_find(int n, F f) : par(n), size(n, 1), f(f) {\n iota(par.begin(), par.end(), 0);\n }\n int find(int i) {\n return par[i] == i ? i : find(par[i]);\n }\n void unite(int i, int j) {\n i = find(i), j = find(j);\n if (i == j)\n return;\n if (size[i] < size[j])\n swap(i, j);\n f(i, j);\n size[i] += size[j];\n par[j] = i;\n }\n};\n\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int> &v, vector<vector<int>> &e) {\n int n = v.size(), m = e.size();\n vector<vector<int>> g(n);\n for (int i = 0; i < m; i++) {\n g[e[i][0]].push_back(e[i][1]);\n g[e[i][1]].push_back(e[i][0]);\n }\n vector ord(n, 0);\n iota(ord.begin(), ord.end(), 0);\n ranges::sort(ord.begin(), ord.end(), [&](int i, int j) {\n return v[i] < v[j];\n });\n int ans = n;\n vector cnt(n, 1);\n union_find uf(n, [&](int i, int j) {\n cnt[i] += cnt[j];\n });\n for (int i = 0, j = 0; i < n;) {\n for (; j < n && v[ord[j]] == v[ord[i]]; j++) {\n for (int k : g[ord[j]]) {\n if (v[k] <= v[ord[j]]) {\n uf.unite(k, ord[j]);\n }\n }\n }\n int cur = 0;\n for (int j2 = i; j2 < j; j2++) {\n cur += cnt[uf.find(ord[j2])] - 1;\n }\n ans += cur / 2;\n for (int j2 = i; j2 < j; j2++) {\n cnt[uf.find(ord[j2])] = 0;\n }\n i = j;\n }\n return ans;\n }\n};\n// ----- CHANGE FOR PROBLEM -----\n\n#ifdef LC_LOCAL\nint main() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n exec(&Solution::numberOfGoodPaths); // CHANGE FOR PROBLEM\n}\n#endif", "memory": "167382" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "auto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n const int n = vals.size();\n vector<vector<int>> tree(n);\n\n for (auto& e: edges) {\n tree[e[0]].push_back(e[1]);\n tree[e[1]].push_back(e[0]);\n }\n\n vector<int> uf(n);\n vector<int> count(n, 1);\n\n iota(uf.begin(), uf.end(), 0);\n\n vector<int> inds(n);\n iota(inds.begin(), inds.end(), 0);\n\n sort(inds.begin(), inds.end(), [&]( int a, int b) {\n return vals[a] < vals[b];\n });\n\n int ans = n;\n for (int id: inds) {\n int p = getRoot(uf, id);\n\n for (int nei: tree[id]) {\n if (vals[nei] <= vals[id]) {\n nei = getRoot(uf, nei);\n if (p != nei) {\n if (vals[nei] == vals[id] && count[nei] > count[p]) {\n swap(nei, p);\n }\n\n uf[nei] = p;\n\n if (vals[nei] == vals[id]) {\n ans += count[nei]*count[p];\n count[p] += count[nei];\n }\n }\n }\n }\n }\n\n return ans;\n }\n\nprivate:\n int getRoot(vector<int>& uf, int id) {\n if (uf[id] != id) {\n uf[id] = getRoot(uf, uf[id]);\n }\n\n return uf[id];\n }\n};", "memory": "167382" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int findp(vector<int>& parent, int a) {\n if ( parent[a] == a ) return a;\n return parent[a] = findp(parent, parent[a]);\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n vector<int> parent(n);\n map<int, int> cnt;\n for ( auto i = 0; i < n; i++ ) {\n cnt[i] = 1;\n parent[i] = i;\n }\n sort(edges.begin(), edges.end(), [&](const vector<int>& a, const vector<int>& b){\n int m1 = max(vals[a[0]], vals[a[1]]);\n int m2 = max(vals[b[0]], vals[b[1]]);\n return m1 < m2;\n });\n int ans = 0;\n for ( auto e: edges ) {\n int a = e[0], b = e[1];\n int p1 = findp(parent, a), p2 = findp(parent, b);\n if ( vals[p1] == vals[p2] ) {\n ans += cnt[p1]*cnt[p2];\n parent[p1] = p2;\n cnt[p2] += cnt[p1];\n } else {\n if ( vals[p1] > vals[p2] ) parent[p2] = p1;\n else parent[p1] = p2;\n }\n }\n\n return ans + n;\n }\n};", "memory": "169315" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n\t\tconst int n = vals.size();\n\t\tvector<vector<int>> g(n);\n\n\t\tfor (auto& e: edges) {\n\t\t\tg[e[0]].push_back(e[1]);\n\t\t\tg[e[1]].push_back(e[0]);\n\t\t}\n\n\t\tvector<int> ids(n), size(n), fa(n);\n\n\t\tiota(begin(ids), end(ids), 0);\n\t\tiota(begin(fa), end(fa), 0);\n\t\tfill(begin(size), end(size), 1);\n\n\t\tfunction<int(int)> find = [&](int x) {\n\t\t\twhile (x != fa[x]) {\n\t\t\t\tx = fa[x] = find(fa[x]);\n\t\t\t}\n\n\t\t\treturn x;\n\t\t};\n\n\t\tint res = n;\n\n\t\tsort(begin(ids), end(ids), [&](auto& a, auto& b) {\n\t\t\treturn vals[a] < vals[b];\n\t\t});\n\n\t\tfor (auto& x: ids) {\n\t\t\tint vx = vals[x];\n\t\t\tint fx = find(x);\n\n\t\t\tfor (auto y: g[x]) {\n\t\t\t\ty = find(y);\n\n\t\t\t\tif (vx < vals[y] || fx == y) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tif (vx == vals[y]) {\n\t\t\t\t\tres += size[fx] * size[y];\n\t\t\t\t\tsize[fx] += size[y];\n\t\t\t\t}\n\n\t\t\t\tfa[y] = fx;\n\t\t\t}\n\t\t}\n\n\t\treturn res;\n }\n};", "memory": "169315" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int find(vector<int> &parent, int idx){\n if(parent[idx]!=idx){\n parent[idx]=find(parent, parent[idx]);\n }\n return parent[idx];\n }\n \n void unions(vector<int>& parent, vector<int>& rank, int idx1, int idx2, int &ans, vector<int>& val){\n int par1=find(parent, idx1);\n int par2=find(parent, idx2);\n if(par1!=par2){\n if(val[par1]==val[par2]){\n ans+=(rank[par1]*rank[par2]);\n rank[par1]+=rank[par2];\n }\n parent[par2]=par1;\n }\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n vector<pair<int,int>> nodes;\n for(int i=0;i<vals.size();i++){\n nodes.push_back(make_pair(vals[i], i));\n }\n vector<vector<int>> nedge(vals.size());\n for(int i=0;i<edges.size();i++){\n \n nedge[edges[i][0]].push_back(edges[i][1]);\n nedge[edges[i][1]].push_back(edges[i][0]);\n \n }\n sort(nodes.begin(), nodes.end());\n vector<int> parent(vals.size()+1);\n vector<int> rank(vals.size()+1,1);\n int ans=0;\n for(int i=0;i<=vals.size();i++)\n parent[i]=i;\n for(int i=0;i<nodes.size();i++){\n for(int j=0;j<nedge[nodes[i].second].size();j++){\n if(vals[nedge[nodes[i].second][j]]<=nodes[i].first){\n unions(parent, rank, nodes[i].second, nedge[nodes[i].second][j], ans, vals);\n }\n }\n }\n return ans+vals.size();\n }\n};", "memory": "171248" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class DSU {\n private:\n vector<int> parent;\n vector<int> rank;\n public:\n DSU(int n) {\n parent.resize(n);\n rank.resize(n);\n for(int i = 0; i<n; i++) {\n parent[i] = i;\n rank[i] = 1;\n }\n }\n\n int getParent(int node) {\n if(parent[node] == node) {\n return node;\n }\n return parent[node] = getParent(parent[node]);\n }\n\n bool isConnected(int node1, int node2) {\n return getParent(node1) == getParent(node2);\n }\n\n void connect(int node1, int node2) {\n int parent1 = getParent(node1);\n int parent2 = getParent(node2);\n if(parent1 == parent2) {\n return;\n }\n if(rank[parent1] >= rank[parent2]) {\n parent[parent2] = parent1;\n rank[parent1] += rank[parent2];\n }\n else {\n parent[parent1] = parent2;\n rank[parent2] += rank[parent1];\n }\n }\n};\n\nclass Solution {\npublic:\n\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int n = vals.size();\n priority_queue<pair<int,int>> edgeOrder;\n map<int, vector<int>> valMap;\n for(int i = 0; i<edges.size(); i++) {\n int u = edges[i][0];\n int v = edges[i][1];\n edgeOrder.push({-max(vals[u], vals[v]), i});\n }\n for(int i = 0; i<n; i++) {\n if(valMap.find(vals[i]) == valMap.end()) {\n valMap[vals[i]] = vector<int>();\n }\n valMap[vals[i]].push_back(i);\n }\n if(valMap.size() == 1) {\n return n * (n + 1)/2;\n }\n DSU dsu(n);\n int ans = n;\n map<int,vector<int>>::iterator it = valMap.begin();\n while(it != valMap.end()) {\n int value = (*it).first;\n vector<int> items = (*it).second;\n while(!edgeOrder.empty()) {\n pair<int,int> top = edgeOrder.top();\n if(abs(top.first) <= value) {\n edgeOrder.pop();\n int index = top.second;\n dsu.connect(edges[index][0], edges[index][1]);\n }\n else {\n break;\n }\n }\n if(items.size() > 0) {\n for(int i = 0; i<items.size(); i++) {\n for(int j = i + 1; j<items.size(); j++) {\n if(dsu.isConnected(items[i], items[j])) {\n ++ans;\n // cout<<items[i]<<\":\"<<items[j]<<\" \";\n }\n }\n }\n }\n ++it;\n }\n return ans;\n }\n};", "memory": "171248" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int find_lab(int u, vector<int> &lab) {\n return (u == lab[u] ? u : lab[u] = find_lab(lab[u], lab));\n }\n int ans = 0;\n void unite(int mxv, int u, int v, vector<int> &sz, vector<int> &mx, vector<int> &lab, vector<int> &vals) {\n int paru = find_lab(u, lab);\n int parv = find_lab(v, lab);\n if (paru == parv) return;\n if (mx[paru] < mxv) sz[paru] = 0;\n if (mx[parv] < mxv) sz[parv] = 0;\n if (sz[paru] < sz[parv]) swap(paru, parv);\n ans = ans - (sz[paru] - 1) * sz[paru]/2;\n ans = ans - (sz[parv] - 1) * sz[parv]/2;\n sz[paru] += sz[parv];\n lab[parv] = paru;\n mx[paru] = max(mx[paru], mx[parv]);\n ans = ans + (sz[paru] - 1) * sz[paru]/2;\n // cout << u << \" \" << v << \" \" << sz[paru] << \" \" << ans << \"\\n\";\n return;\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n int N = (int)vals.size();\n vector<int> sz(N, 1);\n vector<int> mx(N, 0);\n vector<int> lab(N, 0);\n vector<vector<int>> G(N);\n vector<pair<int, int>> sortedVals;\n for (int i = 0; i < N; i ++) {\n sortedVals.push_back(make_pair(vals[i], i));\n lab[i] = i;\n mx[i] = vals[i];\n }\n for (int i = 0; i < (int)edges.size(); i ++) {\n G[edges[i][0]].push_back(edges[i][1]);\n G[edges[i][1]].push_back(edges[i][0]);\n }\n sort(sortedVals.begin(), sortedVals.end());\n for (auto [x, u] : sortedVals) {\n for (auto v : G[u]) {\n if (vals[v] <= x) {\n // cout << u << \" \" << v << \"\\n\";\n unite(x, u, v, sz, mx, lab, vals);\n }\n }\n }\n return ans + N;\n }\n};", "memory": "173180" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class DisjointSet {\npublic:\n DisjointSet(int n): parent(n, -1), size(n, 1) {}\n\n void add(int x) {\n if (parent[x] < 0) parent[x] = x;\n }\n\n int root(int x) {\n if (parent[x] == x) return x;\n return parent[x] = root(parent[x]);\n }\n\n void merge(int x, int y) {\n add(y);\n int px = root(x), py = root(y);\n if (px == py) return;\n if (size[px] < size[py]) swap(px, py);\n parent[py] = px;\n size[px] += size[py];\n }\n\nprivate:\n vector<int> parent, size;\n};\n\nclass Solution {\npublic:\n template<ranges::view Range>\n int process(Range& r, DisjointSet& sets) {\n unordered_map<int, int> groups;\n int last = -1;\n for (auto [x, y]: r) {\n if (x == last) continue;\n auto p = sets.root(x);\n groups[p]++;\n last = x;\n }\n\n return ranges::fold_left(groups, 0,\n [](int x, auto p) { return x + p.second * (p.second - 1) / 2; });\n }\n\n template<ranges::view Range, typename Pred>\n auto take_while(Range& r, Pred&& pred) {\n auto end = r.begin();\n while (end != r.end() && pred(*end)) ++end;\n return ranges::subrange(r.begin(), end);\n }\n\n int numberOfGoodPaths(const vector<int>& vals, const vector<vector<int>>& raw_edges) {\n int n = vals.size();\n vector<pair<int, int>> edges;\n DisjointSet sets(n);\n\n for (auto& v: raw_edges) {\n int x = v[0], y = v[1];\n\n if (vals[x] > vals[y]) edges.emplace_back(x, y);\n else if (vals[x] < vals[y]) edges.emplace_back(y, x);\n else {\n edges.emplace_back(x, y);\n edges.emplace_back(y, x);\n }\n }\n ranges::sort(edges,\n [&](auto e1, auto e2) {\n return pair{vals[e1.first], e1.first} < pair{vals[e2.first], e2.first};\n });\n\n int ans = n;\n for (auto r = ranges::subrange(edges); !r.empty(); ) {\n int v = vals[r.begin()->first];\n auto r2 = take_while(r, [v, &vals](auto e) { return vals[e.first] == v; });\n\n for (auto [x, y]: r2) {\n sets.add(x);\n sets.merge(x, y);\n }\n ans += process(r2, sets);\n\n r.advance(r2.size());\n }\n\n return ans;\n }\n};", "memory": "173180" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class dsu{\n public:\n vector<int>par,siz;\n dsu(int n){\n par.resize(n);\n siz.resize(n,1);\n for(int i=0;i<n;i++) par[i]=i;\n } \n int findUpar(int u)\n {\n if(par[u]==u) return u;\n return par[u]=findUpar(par[u]);\n }\n void unio(int u,int v)\n {\n int up=findUpar(u),vp=findUpar(v);\n if(up==vp) return;\n if(siz[up]<siz[vp])\n {\n siz[vp]+=siz[up];\n par[up]=vp;\n }\n else{\n siz[up]+=siz[vp];\n par[vp]=up;\n }\n }\n};\nclass Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n vector<pair<int,int>>vc;\n int n=vals.size();\n vector<int>adj[n];\n for(int i=0;i<edges.size();i++)\n {\n adj[edges[i][0]].push_back(edges[i][1]);\n adj[edges[i][1]].push_back(edges[i][0]);\n }\n for(int i=0;i<vals.size();i++) vc.push_back({vals[i],i});\n sort(vc.begin(),vc.end(),[](const auto&v1,const auto&v2){return v1.first<v2.first;});\n int ans=0;\n dsu obj(n);\n for(int i=0;i<n;i++)\n {\n int l;\n for(l=i;l<n;l++){\n int x=vc[l].second;\n if(vc[l].first!=vc[i].first) break;\n for(int j=0;j<adj[x].size();j++)\n {\n if(vals[adj[x][j]]<=vc[l].first){\n obj.unio(vc[l].second,adj[x][j]);\n }\n }\n }\n map<int,int>mp;\n for(int k=i;k<l;k++) mp[obj.findUpar(vc[k].second)]++;\n for(auto it:mp)\n {\n ans+=((it.second*(it.second-1))/2);\n }\n i=l-1;\n }\n return ans+n;\n }\n};", "memory": "175113" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class DisjointSet {\n\npublic:\n DisjointSet(int n) {\n parent.resize(n, -1);\n count.resize(n, -1);\n \n }\n\n int Union(int v1, int v2) {\n int p1 = SearhAndCompress(v1);\n int p2 = SearhAndCompress(v2);\n if (p1 == p2) {\n return p1;\n }\n if (count[p1] >= count[p2]) {\n count[p1] += count[p2];\n parent[p2] = p1;\n return p1;\n \n }\n else {\n count[p2] += count[p1];\n parent[p1] = p2;\n return p2;\n }\n }\n\n int SearhAndCompress(int v) {\n int p = v;\n while (parent[p] != -1) {\n p = parent[p];\n }\n\n int s = v;\n while (s != p) {\n int next_s = parent[s];\n parent[s] = p;\n s = next_s;\n }\n return p;\n }\n\n\n // DisjointSet* Searh() {\n \n // DisjointSet* p = this;\n // while (p->parent) {\n // p = p->parent;\n // }\n // return p;\n // }\n\n // int label;\nprivate:\n vector<int> parent;\n vector<int> count;\n};\n\n\nclass Solution {\npublic:\n\n int numberOfGoodPaths(const vector<int>& vals, const vector<vector<int>>& edges) {\n int n = vals.size();\n int m = edges.size();\n vector<vector<int>> graph(n);\n for (const auto &e : edges) {\n int v1 = e[0];\n int v2 = e[1];\n graph[v1].push_back(v2);\n graph[v2].push_back(v1);\n }\n\n DisjointSet g(n);\n vector<pair<int, int>> vals_index(n);\n for (int i = 0; i < n; ++i) {\n vals_index[i] = {vals[i], i};\n }\n sort(vals_index.begin(), vals_index.end());\n int start_i = 0;\n unordered_map<int, int> counter;\n int unioned_v = 0;\n int res = n;\n for (int i = 0; i < n; ++i) {\n int v1 = vals_index[i].second;\n for (int u = 0; u < graph[v1].size(); ++u) {\n int v2 = graph[v1][u];\n if (vals[v1] >= vals[v2]) {\n g.Union(v1, v2);\n }\n }\n\n if (i == n - 1 || vals_index[i].first != vals_index[i + 1].first ) {\n //counter.clear();\n for (int u = unioned_v; u <= i; ++u) {\n int v1 = vals_index[u].second;\n int label = g.SearhAndCompress(v1);\n ++counter[label * n + i];\n } \n \n \n unioned_v = i + 1;\n }\n }\n for (auto e : counter) {\n res += e.second * (e.second - 1) / 2;\n }\n return res;\n\n }\n};", "memory": "175113" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class DisjointSet {\n\npublic:\n DisjointSet(int n) {\n parent.resize(n, -1);\n count.resize(n, -1);\n \n }\n\n int Union(int v1, int v2) {\n int p1 = SearhAndCompress(v1);\n int p2 = SearhAndCompress(v2);\n if (p1 == p2) {\n return p1;\n }\n if (count[p1] >= count[p2]) {\n count[p1] += count[p2];\n parent[p2] = p1;\n return p1;\n \n }\n else {\n count[p2] += count[p1];\n parent[p1] = p2;\n return p2;\n }\n }\n\n int SearhAndCompress(int v) {\n int p = v;\n while (parent[p] != -1) {\n p = parent[p];\n }\n\n int s = v;\n while (s != p) {\n int next_s = parent[s];\n parent[s] = p;\n s = next_s;\n }\n return p;\n }\n\n\n // DisjointSet* Searh() {\n \n // DisjointSet* p = this;\n // while (p->parent) {\n // p = p->parent;\n // }\n // return p;\n // }\n\n // int label;\nprivate:\n vector<int> parent;\n vector<int> count;\n};\n\n\nclass Solution {\npublic:\n\n int numberOfGoodPaths(const vector<int>& vals, const vector<vector<int>>& edges) {\n int n = vals.size();\n int m = edges.size();\n vector<vector<int>> graph(n);\n for (const auto &e : edges) {\n int v1 = e[0];\n int v2 = e[1];\n graph[v1].push_back(v2);\n graph[v2].push_back(v1);\n }\n\n DisjointSet g(n);\n vector<pair<int, int>> vals_index(n);\n for (int i = 0; i < n; ++i) {\n vals_index[i] = {vals[i], i};\n }\n sort(vals_index.begin(), vals_index.end());\n int start_i = 0;\n unordered_map<int, int> counter;\n int unioned_v = 0;\n int res = n;\n for (int i = 0; i < n; ++i) {\n int v1 = vals_index[i].second;\n for (int u = 0; u < graph[v1].size(); ++u) {\n int v2 = graph[v1][u];\n if (vals[v1] >= vals[v2]) {\n g.Union(v1, v2);\n }\n }\n\n if (i == n - 1 || vals_index[i].first != vals_index[i + 1].first ) {\n //counter.clear();\n for (int u = unioned_v; u <= i; ++u) {\n int v1 = vals_index[u].second;\n int label = g.SearhAndCompress(v1);\n ++counter[label * n + i];\n } \n \n \n unioned_v = i + 1;\n }\n }\n for (auto e : counter) {\n res += e.second * (e.second - 1) / 2;\n }\n return res;\n\n }\n};", "memory": "177045" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class DisjointSet {\n\npublic:\n DisjointSet(int n) {\n parent.resize(n, -1);\n count.resize(n, -1);\n \n }\n\n int Union(int v1, int v2) {\n int p1 = SearhAndCompress(v1);\n int p2 = SearhAndCompress(v2);\n if (p1 == p2) {\n return p1;\n }\n if (count[p1] >= count[p2]) {\n count[p1] += count[p2];\n parent[p2] = p1;\n return p1;\n \n }\n else {\n count[p2] += count[p1];\n parent[p1] = p2;\n return p2;\n }\n }\n\n int SearhAndCompress(int v) {\n int p = v;\n while (parent[p] != -1) {\n p = parent[p];\n }\n\n int s = v;\n while (s != p) {\n int next_s = parent[s];\n parent[s] = p;\n s = next_s;\n }\n return p;\n }\n\n\n // DisjointSet* Searh() {\n \n // DisjointSet* p = this;\n // while (p->parent) {\n // p = p->parent;\n // }\n // return p;\n // }\n\n // int label;\nprivate:\n vector<int> parent;\n vector<int> count;\n};\n\n\nclass Solution {\npublic:\n\n int numberOfGoodPaths(const vector<int>& vals, const vector<vector<int>>& edges) {\n int n = vals.size();\n int m = edges.size();\n vector<vector<int>> graph(n);\n for (const auto &e : edges) {\n int v1 = e[0];\n int v2 = e[1];\n graph[v1].push_back(v2);\n graph[v2].push_back(v1);\n }\n\n DisjointSet g(n);\n vector<pair<int, int>> vals_index(n);\n for (int i = 0; i < n; ++i) {\n vals_index[i] = {vals[i], i};\n }\n sort(vals_index.begin(), vals_index.end());\n int start_i = 0;\n unordered_map<int, int> counter;\n int unioned_v = 0;\n int res = n;\n for (int i = 0; i < n; ++i) {\n int v1 = vals_index[i].second;\n for (int u = 0; u < graph[v1].size(); ++u) {\n int v2 = graph[v1][u];\n if (vals[v1] >= vals[v2]) {\n g.Union(v1, v2);\n }\n }\n\n if (i == n - 1 || vals_index[i].first != vals_index[i + 1].first ) {\n //counter.clear();\n for (int u = unioned_v; u <= i; ++u) {\n int v1 = vals_index[u].second;\n int label = g.SearhAndCompress(v1);\n ++counter[label * n + i];\n } \n \n \n unioned_v = i + 1;\n }\n }\n for (auto e : counter) {\n res += e.second * (e.second - 1) / 2;\n }\n return res;\n\n }\n};", "memory": "177045" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": " class Solution {\n\tpublic:\n\t\tint find(vector<int>& parent,int a) //Finds parent of a\n\t\t{\n\t\t\tif(a==parent[a])\n\t\t\t\treturn a;\n\t\t\tparent[a]=find(parent,parent[a]);\n\t\t\treturn parent[a];\n\n\t\t}\n\t\tint numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n sort(edges.begin(),edges.end(),[&](const vector<int>& a,vector<int>& b)\n\t\t\t\t {\n\t\t\t\t\t int m = max(vals[a[0]],vals[a[1]]);\n\t\t\t\t\t int n = max(vals[b[0]],vals[b[1]]);\n\t\t\t\t\t return m<n;\n\t\t\t\t });\n\n\t\t\tint n = vals.size();\n\t\t\tvector<int> parent(n);\n\t\t\tmap<int,int> max_element;\n\t\t\tmap<int,int> count;\n\n\t\t\tfor(int i=0;i<n;i++){\n\t\t\t\tparent[i]=i;\n\t\t\t\tmax_element[i]=vals[i];\n\t\t\t\tcount[i]=1;\n\t\t\t}\n\n\t\t\t\n\n\t\t\tint ans=n;\n\n\t\t\tfor(auto& edge: edges)\n\t\t\t{\n\t\t\t\tint a=find(parent,edge[0]);\n\t\t\t\tint b=find(parent,edge[1]);\n\t\t\t\tif(max_element[a]!=max_element[b])\n\t\t\t\t{\n\t\t\t\t\tif(max_element[a]>max_element[b])\n\t\t\t\t\t{\n\t\t\t\t\t\tparent[b]=a;\n\t\t\t\t\t}\n\t\t\t\t\telse{\n\t\t\t\t\t\tparent[a]=b;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tans += count[a]*count[b];\n count[b] += count[a];\n parent[a] = b;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn ans;\n\t\t}\n\t};", "memory": "178978" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class Solution {\npublic:\n\n int find(vector<int> & parent,int a){\n if(parent[a] == a) return a;\n parent[a] = find(parent, parent[a]);\n return parent[a];\n }\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n sort(edges.begin(),edges.end(),[&](const vector<int>& a,vector<int>& b)\n\t\t\t\t {\n\t\t\t\t\t int m = max(vals[a[0]],vals[a[1]]);\n\t\t\t\t\t int n = max(vals[b[0]],vals[b[1]]);\n\t\t\t\t\t return m<n;\n\t\t\t\t });\n int n = vals.size(),i,j;\n vector<int> parent(n,0); \n map<int, int> max_element; \n map<int, int> count; \n for(i=0;i<n;i++) {\n parent[i] = i;\n max_element[i] = vals[i];\n count[i] = 1;\n }\n int a, b;\n int ans = n;\n for(auto &edge : edges){\n a = find(parent, edge[0]);\n b = find(parent, edge[1]);\n \n if (max_element[a]!=max_element[b]){\n if (max_element[a]>max_element[b]){\n parent[b] = a;\n\n }\n else parent[a] = b; \n }\n else {\n ans += count[a]*count[b];\n count[b] += count[a];\n parent[a] = b;\n }\n\n }\n return ans;\n\n\n \n }\n};", "memory": "178978" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class DisjointSet {\npublic:\n DisjointSet(int n): parent(n, -1), size(n, 1) {}\n\n void add(int x) {\n if (parent[x] < 0) parent[x] = x;\n }\n\n int root(int x) {\n if (parent[x] == x) return x;\n return parent[x] = root(parent[x]);\n }\n\n void merge(int x, int y) {\n add(y);\n int px = root(x), py = root(y);\n if (px == py) return;\n if (size[px] < size[py]) swap(px, py);\n parent[py] = px;\n size[px] += size[py];\n }\n\nprivate:\n vector<int> parent, size;\n};\n\nclass Solution {\npublic:\n template<ranges::view Range>\n int process(Range& r, DisjointSet& sets) {\n unordered_map<int, int> groups;\n int last = -1;\n for (auto [x, y]: r) {\n if (x == last) continue;\n auto p = sets.root(x);\n groups[p]++;\n last = x;\n }\n\n return ranges::fold_left(groups, 0,\n [](int x, pair<int, int> p) { return x + p.second * (p.second - 1) / 2; });\n }\n\n template<ranges::view Range, typename Pred>\n auto take_while(Range& r, Pred&& pred) {\n auto end = r.begin();\n while (end != r.end() && pred(*end)) ++end;\n return ranges::subrange(r.begin(), end);\n }\n\n int numberOfGoodPaths(const vector<int>& vals, const vector<vector<int>>& raw_edges) {\n int n = vals.size();\n vector<pair<int, int>> edges;\n DisjointSet sets(n);\n\n for (auto& v: raw_edges) {\n int x = v[0], y = v[1];\n\n edges.emplace_back(x, y);\n edges.emplace_back(y, x);\n }\n sort(edges.begin(), edges.end(),\n [&](auto e1, auto e2) {\n auto v1 = vals[e1.first], v2 = vals[e2.first];\n if (v1 == v2) return e1.first < e2.first;\n return v1 < v2;\n });\n\n int ans = n;\n for (auto r = ranges::subrange(edges); !r.empty(); ) {\n int v = vals[r.begin()->first];\n auto r2 = take_while(r, [v, &vals](auto e) { return vals[e.first] == v; });\n\n for (auto [x, y]: r2) {\n sets.add(x);\n if (vals[y] <= v) sets.merge(x, y);\n }\n ans += process(r2, sets);\n\n r.advance(r2.size());\n }\n\n return ans;\n }\n};", "memory": "180910" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class DisjointSet {\npublic:\n DisjointSet(int n): parent(n, -1), size(n, 1) {}\n\n void add(int x) {\n if (parent[x] < 0) parent[x] = x;\n }\n\n int root(int x) {\n if (parent[x] == x) return x;\n return parent[x] = root(parent[x]);\n }\n\n void merge(int x, int y) {\n add(y);\n int px = root(x), py = root(y);\n if (px == py) return;\n if (size[px] < size[py]) swap(px, py);\n parent[py] = px;\n size[px] += size[py];\n }\n\nprivate:\n vector<int> parent, size;\n};\n\nclass Solution {\npublic:\n template<ranges::view Range>\n int process(Range& r, DisjointSet& sets) {\n unordered_map<int, int> groups;\n int last = -1;\n for (auto [x, y]: r) {\n if (x == last) continue;\n auto p = sets.root(x);\n groups[p]++;\n last = x;\n }\n\n return ranges::fold_left(groups, 0,\n [](int x, auto p) { return x + p.second * (p.second - 1) / 2; });\n }\n\n template<ranges::view Range, typename Pred>\n auto take_while(Range& r, Pred&& pred) {\n auto end = r.begin();\n while (end != r.end() && pred(*end)) ++end;\n return ranges::subrange(r.begin(), end);\n }\n\n int numberOfGoodPaths(const vector<int>& vals, const vector<vector<int>>& raw_edges) {\n int n = vals.size();\n vector<pair<int, int>> edges;\n DisjointSet sets(n);\n\n for (auto& v: raw_edges) {\n int x = v[0], y = v[1];\n\n edges.emplace_back(x, y);\n edges.emplace_back(y, x);\n }\n sort(edges.begin(), edges.end(),\n [&](auto e1, auto e2) {\n return pair{vals[e1.first], e1.first} < pair{vals[e2.first], e2.first};\n });\n\n int ans = n;\n for (auto r = ranges::subrange(edges); !r.empty(); ) {\n int v = vals[r.begin()->first];\n auto r2 = take_while(r, [v, &vals](auto e) { return vals[e.first] == v; });\n\n for (auto [x, y]: r2) {\n sets.add(x);\n if (vals[y] <= v) sets.merge(x, y);\n }\n ans += process(r2, sets);\n\n r.advance(r2.size());\n }\n\n return ans;\n }\n};", "memory": "180910" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class DisjointSet {\npublic:\n DisjointSet(int n): parent(n, -1), size(n) {}\n\n void add(int x) {\n parent[x] = x;\n size[x] = 1;\n }\n\n int root(int x) {\n if (parent[x] == x) return x;\n return parent[x] = root(parent[x]);\n }\n\n void merge(int x, int y) {\n if (parent[y] < 0) return;\n\n int px = root(x), py = root(y);\n if (size[px] < size[py]) {\n swap(px, py);\n }\n parent[py] = px;\n size[px] += size[py];\n }\n\nprivate:\n vector<int> parent, size;\n};\n\nclass Solution {\npublic:\n template<typename Iterator>\n int process(pair<Iterator, Iterator> range, DisjointSet& sets) {\n unordered_map<int, int> groups;\n for (auto x = range.first; x != range.second; ++x) {\n auto p = sets.root(*x);\n groups[p]++;\n }\n int sum = 0;\n for (auto [k, v]: groups) {\n sum += v * (v - 1) / 2;\n }\n return sum;\n }\n\n int numberOfGoodPaths(vector<int>& vals, const vector<vector<int>>& raw_edges) {\n int n = vals.size();\n vector<pair<int, int>> edges;\n DisjointSet sets(n);\n\n for (auto& v: raw_edges) {\n int x = v[0], y = v[1];\n\n edges.emplace_back(x, y);\n edges.emplace_back(y, x);\n }\n sort(edges.begin(), edges.end());\n\n vector<int> nodes(n);\n for (int i = 0; i < n; i++) nodes[i] = i;\n sort(nodes.begin(), nodes.end(), [&vals](int x, int y) { return vals[x] < vals[y]; });\n\n int ans = n;\n vector<int>::iterator begin = nodes.begin(), end = nodes.begin();\n for (auto it2 = nodes.begin(); it2 != nodes.end(); ++it2) {\n int x = *it2;\n if (end - begin && vals[x] != vals[*begin]) {\n ans += process(pair{begin, end}, sets);\n begin = end = it2;\n }\n ++end;\n\n sets.add(x);\n for (auto it = lower_bound(edges.begin(), edges.end(), pair{x, -1});\n it != edges.end() && it->first == x; ++it) {\n sets.merge(x, it->second);\n }\n }\n ans += process(pair{begin, end}, sets);\n\n return ans;\n }\n};", "memory": "182843" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
0
{ "code": "class DisjointSet {\npublic:\n DisjointSet(int n): parent(n, -1), size(n) {}\n\n void add(int x) {\n parent[x] = x;\n size[x] = 1;\n }\n\n int root(int x) {\n if (parent[x] == x) return x;\n return parent[x] = root(parent[x]);\n }\n\n void merge(int x, int y) {\n if (parent[y] < 0) return;\n\n int px = root(x), py = root(y);\n if (size[px] < size[py]) {\n swap(px, py);\n }\n parent[py] = px;\n size[px] += size[py];\n }\n\nprivate:\n vector<int> parent, size;\n};\n\nclass Solution {\npublic:\n template<typename Range>\n int process(Range r, DisjointSet& sets) {\n unordered_map<int, int> groups;\n for (auto x: r) {\n auto p = sets.root(x);\n groups[p]++;\n }\n int sum = 0;\n for (auto [k, v]: groups) {\n sum += v * (v - 1) / 2;\n }\n return sum;\n }\n\n int numberOfGoodPaths(vector<int>& vals, const vector<vector<int>>& raw_edges) {\n int n = vals.size();\n vector<pair<int, int>> edges;\n DisjointSet sets(n);\n\n for (auto& v: raw_edges) {\n int x = v[0], y = v[1];\n\n edges.emplace_back(x, y);\n edges.emplace_back(y, x);\n }\n sort(edges.begin(), edges.end());\n\n vector<int> nodes(n);\n for (int i = 0; i < n; i++) nodes[i] = i;\n sort(nodes.begin(), nodes.end(), [&vals](int x, int y) { return vals[x] < vals[y]; });\n\n int ans = n;\n vector<int>::iterator begin = nodes.begin(), end = nodes.begin();\n for (auto it2 = nodes.begin(); it2 != nodes.end(); ++it2) {\n int x = *it2;\n if (end - begin && vals[x] != vals[*begin]) {\n ans += process(ranges::subrange{begin, end}, sets);\n begin = end = it2;\n }\n ++end;\n\n sets.add(x);\n for (auto it = lower_bound(edges.begin(), edges.end(), pair{x, -1});\n it != edges.end() && it->first == x; ++it) {\n sets.merge(x, it->second);\n }\n }\n ans += process(ranges::subrange{begin, end}, sets);\n\n return ans;\n }\n};", "memory": "182843" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class DisjointSet {\n\npublic:\n DisjointSet(int label) : label(label), parent(nullptr), count(1) {\n }\n\n void* Union(DisjointSet* other) {\n DisjointSet* p1 = this->SearhAndCompress();\n DisjointSet* p2 = other->SearhAndCompress();\n if (p1 == p2) {\n return p1;\n }\n if (p1->count >= p2->count) {\n p1->count += p2->count;\n p2->parent = p1;\n return p1;\n \n }\n else {\n p2->count += p1->count;\n p1->parent = p2;\n return p2;\n }\n }\n\n DisjointSet* SearhAndCompress() {\n \n DisjointSet* p = this;\n while (p->parent) {\n p = p->parent;\n }\n\n DisjointSet* s = this;\n while (s != p) {\n DisjointSet* next_s = s->parent;\n s->parent = p;\n s = next_s;\n }\n return p;\n }\n\n\n DisjointSet* Searh() {\n \n DisjointSet* p = this;\n while (p->parent) {\n p = p->parent;\n }\n return p;\n }\n\n int label;\nprivate:\n int count;\n \n DisjointSet* parent;\n};\n\n\nclass Solution {\npublic:\n\n int numberOfGoodPaths(const vector<int>& vals, const vector<vector<int>>& edges) {\n int n = vals.size();\n int m = edges.size();\n vector<vector<int>> graph(n);\n for (const auto &e : edges) {\n int v1 = e[0];\n int v2 = e[1];\n graph[v1].push_back(v2);\n graph[v2].push_back(v1);\n }\n\n vector<DisjointSet*> g(n);\n for (int i = 0; i < n; ++i) {\n g[i] = new DisjointSet(i);\n }\n vector<pair<int, int>> vals_index(n);\n for (int i = 0; i < n; ++i) {\n vals_index[i] = {vals[i], i};\n }\n sort(vals_index.begin(), vals_index.end());\n int start_i = 0;\n unordered_map<int, int> counter;\n int unioned_v = -1;\n int res = n;\n for (int i = 0; i < n; ++i) {\n int v1 = vals_index[i].second;\n for (int u = 0; u < graph[v1].size(); ++u) {\n int v2 = graph[v1][u];\n if (vals[v1] >= vals[v2]) {\n g[v1]->Union(g[v2]);\n }\n }\n if (unioned_v == -1) {\n unioned_v = i;\n }\n\n if (i == n - 1 || vals_index[i].first != vals_index[i + 1].first ) {\n counter.clear();\n for (int u = unioned_v; u <= i; ++u) {\n int v1 = vals_index[u].second;\n int label = g[v1]->SearhAndCompress()->label;\n ++counter[label];\n } \n for (auto e : counter) {\n res += e.second * (e.second - 1) / 2;\n }\n \n unioned_v = -1;\n }\n }\n return res;\n\n }\n};", "memory": "184775" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class DisjointSet {\n\npublic:\n DisjointSet(int label) : label(label), parent(nullptr), count(1) {\n }\n\n void* Union(DisjointSet* other) {\n DisjointSet* p1 = this->SearhAndCompress();\n DisjointSet* p2 = other->SearhAndCompress();\n if (p1 == p2) {\n return p1;\n }\n if (p1->count >= p2->count) {\n p1->count += p2->count;\n p2->parent = p1;\n return p1;\n \n }\n else {\n p2->count += p1->count;\n p1->parent = p2;\n return p2;\n }\n }\n\n DisjointSet* SearhAndCompress() {\n \n DisjointSet* p = this;\n while (p->parent) {\n p = p->parent;\n }\n\n DisjointSet* s = this;\n while (s != p) {\n DisjointSet* next_s = s->parent;\n s->parent = p;\n s = next_s;\n }\n return p;\n }\n\n\n DisjointSet* Searh() {\n \n DisjointSet* p = this;\n while (p->parent) {\n p = p->parent;\n }\n return p;\n }\n\n int label;\nprivate:\n int count;\n \n DisjointSet* parent;\n};\n\n\nclass Solution {\npublic:\n\n int numberOfGoodPaths(const vector<int>& vals, const vector<vector<int>>& edges) {\n int n = vals.size();\n int m = edges.size();\n vector<vector<int>> graph(n);\n for (const auto &e : edges) {\n int v1 = e[0];\n int v2 = e[1];\n graph[v1].push_back(v2);\n graph[v2].push_back(v1);\n }\n\n vector<DisjointSet*> g(n);\n for (int i = 0; i < n; ++i) {\n g[i] = new DisjointSet(i);\n }\n vector<pair<int, int>> vals_index(n);\n for (int i = 0; i < n; ++i) {\n vals_index[i] = {vals[i], i};\n }\n sort(vals_index.begin(), vals_index.end());\n int start_i = 0;\n unordered_map<int, int> counter;\n int unioned_v = 0;\n int res = n;\n for (int i = 0; i < n; ++i) {\n int v1 = vals_index[i].second;\n for (int u = 0; u < graph[v1].size(); ++u) {\n int v2 = graph[v1][u];\n if (vals[v1] >= vals[v2]) {\n g[v1]->Union(g[v2]);\n }\n }\n\n if (i == n - 1 || vals_index[i].first != vals_index[i + 1].first ) {\n counter.clear();\n for (int u = unioned_v; u <= i; ++u) {\n int v1 = vals_index[u].second;\n int label = g[v1]->SearhAndCompress()->label;\n ++counter[label];\n } \n for (auto e : counter) {\n res += e.second * (e.second - 1) / 2;\n }\n \n unioned_v = i + 1;\n }\n }\n return res;\n\n }\n};", "memory": "184775" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class Solution {\npublic:\n struct DSU{\n vector<int> parent;\n vector<int> size;\n\n DSU(int n){\n for(int i = 0; i < n; i++){\n parent.push_back(i);\n size.push_back(1);\n }\n }\n\n int get_parent(int u){\n return parent[u] = u == parent[u] ? u : get_parent(parent[u]);\n }\n\n void merge(int u, int v){\n u = get_parent(u);\n v = get_parent(v);\n if(u == v)\n return ;\n if(size[u] < size[v])\n swap(u, v);\n size[u] += size[v];\n parent[v] = u;\n } \n };\n\n int add(int v, vector<int>& vals, vector<vector<int>>& x, vector<vector<int>>& edges, DSU& g){ \n if(x[v].empty())\n return 0; \n while(!edges.empty() && max(vals[edges.back()[0]], vals[edges.back()[1]]) <= v){\n g.merge(edges.back()[0], edges.back()[1]);\n edges.pop_back();\n }\n\n int res = 0;\n vector<int> t;\n for(int i : x[v])\n t.push_back(g.get_parent(i));\n sort(t.begin(), t.end());\n\n vector<int> vv;\n int c = 1;\n for(int i = 1; i < t.size(); i++){\n if(t[i] != t[i - 1]){\n vv.push_back(c);\n c = 1;\n }\n else\n c++;\n }\n vv.push_back(c);\n\n for(auto i : vv)\n res += (i * (i + 1)) >> 1;\n \n return res;\n }\n\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n for(int i = 0; i < edges.size(); i++)\n edges[i].push_back(max(vals[edges[i][0]], vals[edges[i][1]]));\n\n sort(edges.begin(), edges.end(), [&](vector<int>& a, vector<int>& b){\n\t \treturn a[2] < b[2];\n\t\t});\n reverse(edges.begin(), edges.end());\n \n int mx = *max_element(vals.begin(), vals.end());\n vector<vector<int>> x(mx + 1);\n for(int i = 0; i < vals.size(); i++)\n x[vals[i]].push_back(i);\n\n DSU g(vals.size() + 5);\n int ans = 0;\n for(int i = 0; i <= mx; i++)\n ans += add(i, vals, x, edges, g);\n\n return ans;\n }\n};", "memory": "186708" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int numberOfGoodPaths(vector<int>& vals, vector<vector<int>>& edges) {\n \n int n = vals.size();\n vector<int> par(n),sz(n);\n vector<map<int,int>> freq(n);\n\n auto cmp = [&](vector<int> &a,vector<int> &b){\n return max(vals[a[0]],vals[a[1]])<max(vals[b[0]],vals[b[1]]);\n };\n sort(edges.begin(),edges.end(),cmp);\n\n function<int(int)> find = [&](int a){\n if(par[a]==a)return a;\n return par[a]=find(par[a]);\n };\n\n auto unite = [&](int a,int b){\n a=find(a),b=find(b);\n if(a!=b){\n if(sz[a]<sz[b])swap(a,b);\n par[b]=a;\n sz[a]+=sz[b];\n for(auto x:freq[b]){\n freq[a][x.first]+=x.second;\n }\n }\n };\n\n for(int i=0;i<n;i++){\n par[i]=i;\n sz[i]=1;\n freq[i][vals[i]]=1;\n }\n int ans = 0;\n for(auto &e:edges){\n if(vals[e[0]]<vals[e[1]])swap(e[0],e[1]);\n int val=0;\n int val1=freq[find(e[0])][vals[e[0]]];\n if(freq[find(e[1])].count(vals[e[0]])){\n val=freq[find(e[1])][vals[e[0]]];\n }\n unite(e[0],e[1]);\n ans+=val*val1;\n }\n return ans+n;\n }\n};", "memory": "186708" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class DisjointSet {\n\npublic:\n DisjointSet(int label) : label(label), parent(nullptr), count(1) {\n }\n\n void* Union(DisjointSet* other) {\n DisjointSet* p1 = this->SearhAndCompress();\n DisjointSet* p2 = other->SearhAndCompress();\n if (p1 == p2) {\n return p1;\n }\n if (p1->count >= p2->count) {\n p1->count += p2->count;\n p2->parent = p1;\n return p1;\n \n }\n else {\n p2->count += p1->count;\n p1->parent = p2;\n return p2;\n }\n }\n\n DisjointSet* SearhAndCompress() {\n \n DisjointSet* p = this;\n while (p->parent) {\n p = p->parent;\n }\n\n DisjointSet* s = this;\n while (s != p) {\n DisjointSet* next_s = s->parent;\n s->parent = p;\n s = next_s;\n }\n return p;\n }\n\n\n DisjointSet* Searh() {\n \n DisjointSet* p = this;\n while (p->parent) {\n p = p->parent;\n }\n return p;\n }\n\n int label;\nprivate:\n int count;\n \n DisjointSet* parent;\n};\n\n\nclass Solution {\npublic:\n\n int numberOfGoodPaths(const vector<int>& vals, const vector<vector<int>>& edges) {\n int n = vals.size();\n int m = edges.size();\n vector<vector<int>> graph(n);\n for (const auto &e : edges) {\n int v1 = e[0];\n int v2 = e[1];\n graph[v1].push_back(v2);\n graph[v2].push_back(v1);\n }\n\n vector<DisjointSet*> g(n);\n for (int i = 0; i < n; ++i) {\n g[i] = new DisjointSet(i);\n }\n vector<pair<int, int>> vals_index(n);\n for (int i = 0; i < n; ++i) {\n vals_index[i] = {vals[i], i};\n }\n sort(vals_index.begin(), vals_index.end());\n int start_i = 0;\n unordered_map<int, int> counter;\n int unioned_v = 0;\n int res = n;\n for (int i = 0; i < n; ++i) {\n int v1 = vals_index[i].second;\n for (int u = 0; u < graph[v1].size(); ++u) {\n int v2 = graph[v1][u];\n if (vals[v1] >= vals[v2]) {\n g[v1]->Union(g[v2]);\n }\n }\n\n if (i == n - 1 || vals_index[i].first != vals_index[i + 1].first ) {\n //counter.clear();\n for (int u = unioned_v; u <= i; ++u) {\n int v1 = vals_index[u].second;\n int label = g[v1]->SearhAndCompress()->label;\n ++counter[label * n + i];\n } \n \n \n unioned_v = i + 1;\n }\n }\n for (auto e : counter) {\n res += e.second * (e.second - 1) / 2;\n }\n return res;\n\n }\n};", "memory": "188640" }
2,505
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p> <p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p> <ol> <li>The starting node and the ending node have the <strong>same</strong> value.</li> <li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li> </ol> <p>Return <em>the number of distinct good paths</em>.</p> <p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" /> <pre> <strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]] <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4. (The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.) Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0]. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" /> <pre> <strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]] <strong>Output:</strong> 7 <strong>Explanation:</strong> There are 5 good paths consisting of a single node. There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3. </pre> <p><strong class="example">Example 3:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" /> <pre> <strong>Input:</strong> vals = [1], edges = [] <strong>Output:</strong> 1 <strong>Explanation:</strong> The tree consists of only one node, so there is one good path. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == vals.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= vals[i] &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
1
{ "code": "class DisjointSet {\n\npublic:\n DisjointSet(int label) : label(label), parent(nullptr), count(1) {\n }\n\n void* Union(DisjointSet* other) {\n DisjointSet* p1 = this->SearhAndCompress();\n DisjointSet* p2 = other->SearhAndCompress();\n if (p1 == p2) {\n return p1;\n }\n if (p1->count >= p2->count) {\n p1->count += p2->count;\n p2->parent = p1;\n return p1;\n \n }\n else {\n p2->count += p1->count;\n p1->parent = p2;\n return p2;\n }\n }\n\n DisjointSet* SearhAndCompress() {\n \n DisjointSet* p = this;\n while (p->parent) {\n p = p->parent;\n }\n\n DisjointSet* s = this;\n while (s != p) {\n DisjointSet* next_s = s->parent;\n s->parent = p;\n s = next_s;\n }\n return p;\n }\n\n\n DisjointSet* Searh() {\n \n DisjointSet* p = this;\n while (p->parent) {\n p = p->parent;\n }\n return p;\n }\n\n int label;\nprivate:\n int count;\n \n DisjointSet* parent;\n};\n\n\nclass Solution {\npublic:\n\n int numberOfGoodPaths(const vector<int>& vals, const vector<vector<int>>& edges) {\n int n = vals.size();\n int m = edges.size();\n vector<vector<int>> graph(n);\n for (const auto &e : edges) {\n int v1 = e[0];\n int v2 = e[1];\n graph[v1].push_back(v2);\n graph[v2].push_back(v1);\n }\n\n vector<DisjointSet*> g(n);\n for (int i = 0; i < n; ++i) {\n g[i] = new DisjointSet(i);\n }\n vector<pair<int, int>> vals_index(n);\n for (int i = 0; i < n; ++i) {\n vals_index[i] = {vals[i], i};\n }\n sort(vals_index.begin(), vals_index.end());\n int start_i = 0;\n unordered_map<int, int> counter;\n int unioned_v = 0;\n int res = n;\n for (int i = 0; i < n; ++i) {\n int v1 = vals_index[i].second;\n for (int u = 0; u < graph[v1].size(); ++u) {\n int v2 = graph[v1][u];\n if (vals[v1] >= vals[v2]) {\n g[v1]->Union(g[v2]);\n }\n }\n\n if (i == n - 1 || vals_index[i].first != vals_index[i + 1].first ) {\n //counter.clear();\n for (int u = unioned_v; u <= i; ++u) {\n int v1 = vals_index[u].second;\n int label = g[v1]->SearhAndCompress()->label;\n ++counter[label * n + i];\n } \n \n \n unioned_v = i + 1;\n }\n }\n for (auto e : counter) {\n res += e.second * (e.second - 1) / 2;\n }\n return res;\n\n }\n};", "memory": "188640" }