René's URL Explorer Experiment


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

direct link

Domain: leetcodejavascript.com


Hey, it has json ld scripts:
{"@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"}

authorLeetCodeJavascript.com
og:typearticle
og:imagehttps://leetcodejavascript.com/og-image.jpg
og:site_nameLeetCode JavaScript Solutions
twitter:cardsummary_large_image
twitter:urlhttps://leetcodejavascript.com/solutions/create-components-with-same-value
twitter:imagehttps://leetcodejavascript.com/og-image.jpg
twitter:creator@joshcrozier
theme-color#1f2937

Links:

LeetCodeJavascript.com https://leetcodejavascript.com/
Star on GitHubhttps://github.com/JoshCrozier/leetcode-javascript
Back to all solutionshttps://leetcodejavascript.com
View on LeetCode https://leetcode.com/problems/create-components-with-same-value/
View on GitHubhttps://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
Enumerationhttps://leetcodejavascript.com/tags/enumeration
Josh Crozierhttps://joshcrozier.com

Viewport: width=device-width,initial-scale=1

Robots: index, follow


URLs of crawlers that visited me.