Title: #2440 - Create Components With Same Value - LeetCode JavaScript Solutions
Open Graph Title: #2440 - Create Components With Same Value - LeetCode JavaScript Solutions
X Title: #2440 - Create Components With Same Value - LeetCode JavaScript Solutions
Description: There is an undirected tree with n nodes labeled from 0 to n - 1. You are given a 0-indexed integer array nums of length n where nums[i] rep...
Open Graph Description: There is an undirected tree with n nodes labeled from 0 to n - 1. You are given a 0-indexed integer array nums of length n where nums[i] rep...
X Description: There is an undirected tree with n nodes labeled from 0 to n - 1. You are given a 0-indexed integer array nums of length n where nums[i] rep...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/create-components-with-same-value
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/create-components-with-same-value","name":"#2440 - Create Components With Same Value - LeetCode JavaScript Solutions","description":"There is an undirected tree with n nodes labeled from 0 to n - 1. You are given a 0-indexed integer array nums of length n where nums[i] rep...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number[]} nums\n * @param {number[][]} edges\n * @return {number}\n */\nvar componentValue = function(nums, edges) {\n const n = nums.length;\n const graph = Array.from({ length: n }, () => []);\n const totalSum = nums.reduce((sum, val) => sum + val, 0);\n\n for (const [u, v] of edges) {\n graph[u].push(v);\n graph[v].push(u);\n }\n\n for (let i = n; i >= 1; i--) {\n if (totalSum % i === 0) {\n const target = totalSum / i;\n const [, components] = countNodes(0, -1, target);\n if (components === i) {\n return i - 1;\n }\n }\n }\n\n return 0;\n\n function countNodes(node, parent, target) {\n let sum = nums[node];\n let components = 0;\n\n for (const neighbor of graph[node]) {\n if (neighbor !== parent) {\n const [childSum, childComponents] = countNodes(neighbor, node, target);\n sum += childSum;\n components += childComponents;\n }\n }\n\n if (sum === target) {\n return [0, components + 1];\n }\n\n return [sum, components];\n }\n};","keywords":"LeetCode 2440, #2440 - Create Components With Same Value, Hard, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/create-components-with-same-value |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/create-components-with-same-value/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/2440-create-components-with-same-value.js |
| Array | https://leetcodejavascript.com/tags/array |
| Math | https://leetcodejavascript.com/tags/math |
| Depth-First Search | https://leetcodejavascript.com/tags/depth-first-search |
| Tree | https://leetcodejavascript.com/tags/tree |
| Enumeration | https://leetcodejavascript.com/tags/enumeration |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width,initial-scale=1
Robots: index, follow