Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcd8ab2b

Browse files
committed
Add solution #2440
1 parent770ed7c commitcd8ab2b

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#1,949 LeetCode solutions in JavaScript
1+
#1,950 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1841,6 +1841,7 @@
18411841
2437|[Number of Valid Clock Times](./solutions/2437-number-of-valid-clock-times.js)|Easy|
18421842
2438|[Range Product Queries of Powers](./solutions/2438-range-product-queries-of-powers.js)|Medium|
18431843
2439|[Minimize Maximum of Array](./solutions/2439-minimize-maximum-of-array.js)|Medium|
1844+
2440|[Create Components With Same Value](./solutions/2440-create-components-with-same-value.js)|Hard|
18441845
2444|[Count Subarrays With Fixed Bounds](./solutions/2444-count-subarrays-with-fixed-bounds.js)|Hard|
18451846
2460|[Apply Operations to an Array](./solutions/2460-apply-operations-to-an-array.js)|Easy|
18461847
2462|[Total Cost to Hire K Workers](./solutions/2462-total-cost-to-hire-k-workers.js)|Medium|
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* 2440. Create Components With Same Value
3+
* https://leetcode.com/problems/create-components-with-same-value/
4+
* Difficulty: Hard
5+
*
6+
* There is an undirected tree with n nodes labeled from 0 to n - 1.
7+
*
8+
* You are given a 0-indexed integer array nums of length n where nums[i] represents the value
9+
* of the ith node. You are also given a 2D integer array edges of length n - 1 where
10+
* edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.
11+
*
12+
* You are allowed to delete some edges, splitting the tree into multiple connected components.
13+
* Let the value of a component be the sum of all nums[i] for which node i is in the component.
14+
*
15+
* Return the maximum number of edges you can delete, such that every connected component in
16+
* the tree has the same value.
17+
*/
18+
19+
/**
20+
*@param {number[]} nums
21+
*@param {number[][]} edges
22+
*@return {number}
23+
*/
24+
varcomponentValue=function(nums,edges){
25+
constn=nums.length;
26+
constgraph=Array.from({length:n},()=>[]);
27+
consttotalSum=nums.reduce((sum,val)=>sum+val,0);
28+
29+
for(const[u,v]ofedges){
30+
graph[u].push(v);
31+
graph[v].push(u);
32+
}
33+
34+
for(leti=n;i>=1;i--){
35+
if(totalSum%i===0){
36+
consttarget=totalSum/i;
37+
const[,components]=countNodes(0,-1,target);
38+
if(components===i){
39+
returni-1;
40+
}
41+
}
42+
}
43+
44+
return0;
45+
46+
functioncountNodes(node,parent,target){
47+
letsum=nums[node];
48+
letcomponents=0;
49+
50+
for(constneighborofgraph[node]){
51+
if(neighbor!==parent){
52+
const[childSum,childComponents]=countNodes(neighbor,node,target);
53+
sum+=childSum;
54+
components+=childComponents;
55+
}
56+
}
57+
58+
if(sum===target){
59+
return[0,components+1];
60+
}
61+
62+
return[sum,components];
63+
}
64+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp