Skip to content

Commit 116d52d

Browse files
committed
Add solution #1617
1 parent 3f0558e commit 116d52d

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,423 LeetCode solutions in JavaScript
1+
# 1,424 LeetCode solutions in JavaScript
22

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

@@ -1247,6 +1247,7 @@
12471247
1614|[Maximum Nesting Depth of the Parentheses](./solutions/1614-maximum-nesting-depth-of-the-parentheses.js)|Easy|
12481248
1615|[Maximal Network Rank](./solutions/1615-maximal-network-rank.js)|Medium|
12491249
1616|[Split Two Strings to Make Palindrome](./solutions/1616-split-two-strings-to-make-palindrome.js)|Medium|
1250+
1617|[Count Subtrees With Max Distance Between Cities](./solutions/1617-count-subtrees-with-max-distance-between-cities.js)|Hard|
12501251
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12511252
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12521253
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* 1617. Count Subtrees With Max Distance Between Cities
3+
* https://leetcode.com/problems/count-subtrees-with-max-distance-between-cities/
4+
* Difficulty: Hard
5+
*
6+
* There are n cities numbered from 1 to n. You are given an array edges of size n-1, where
7+
* edges[i] = [ui, vi] represents a bidirectional edge between cities ui and vi. There exists
8+
* a unique path between each pair of cities. In other words, the cities form a tree.
9+
*
10+
* A subtree is a subset of cities where every city is reachable from every other city in the
11+
* subset, where the path between each pair passes through only the cities from the subset.
12+
* Two subtrees are different if there is a city in one subtree that is not present in the other.
13+
*
14+
* For each d from 1 to n-1, find the number of subtrees in which the maximum distance between
15+
* any two cities in the subtree is equal to d.
16+
*
17+
* Return an array of size n-1 where the dth element (1-indexed) is the number of subtrees in
18+
* which the maximum distance between any two cities is equal to d.
19+
*
20+
* Notice that the distance between the two cities is the number of edges in the path between them.
21+
*/
22+
23+
/**
24+
* @param {number} n
25+
* @param {number[][]} edges
26+
* @return {number[]}
27+
*/
28+
var countSubgraphsForEachDiameter = function(n, connections) {
29+
const adjacencyList = Array.from({ length: n }, () => []);
30+
for (const [u, v] of connections) {
31+
adjacencyList[u - 1].push(v - 1);
32+
adjacencyList[v - 1].push(u - 1);
33+
}
34+
35+
const diameterCounts = new Array(n - 1).fill(0);
36+
37+
for (let mask = 1; mask < 1 << n; mask++) {
38+
const selectedNodes = Array(n).fill(0);
39+
let nodeCount = 0;
40+
for (let i = 0; i < n; i++) {
41+
if (mask & (1 << i)) {
42+
selectedNodes[i] = 1;
43+
nodeCount++;
44+
}
45+
}
46+
47+
if (nodeCount < 2) continue;
48+
49+
const start = selectedNodes.findIndex(bit => bit);
50+
const { maxDist: dist1, farthestNode, distances } = findMaxDistance(selectedNodes, start);
51+
52+
if (distances.some((d, i) => selectedNodes[i] && d === -1)) continue;
53+
54+
const { maxDist: dist2 } = findMaxDistance(selectedNodes, farthestNode);
55+
56+
if (dist2 > 0) {
57+
diameterCounts[dist2 - 1]++;
58+
}
59+
}
60+
61+
return diameterCounts;
62+
63+
function findMaxDistance(nodes, start) {
64+
const distances = new Array(n).fill(-1);
65+
const queue = [start];
66+
distances[start] = 0;
67+
let maxDist = 0;
68+
let farthestNode = start;
69+
70+
while (queue.length) {
71+
const current = queue.shift();
72+
for (const neighbor of adjacencyList[current]) {
73+
if (nodes[neighbor] && distances[neighbor] === -1) {
74+
distances[neighbor] = distances[current] + 1;
75+
if (distances[neighbor] > maxDist) {
76+
maxDist = distances[neighbor];
77+
farthestNode = neighbor;
78+
}
79+
queue.push(neighbor);
80+
}
81+
}
82+
}
83+
84+
return { maxDist, farthestNode, distances };
85+
}
86+
};

0 commit comments

Comments
 (0)