Skip to content

Commit aa4d846

Browse files
author
Eric
committed
101차 3번 문제풀이
1 parent eeb3a55 commit aa4d846

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function solution(n, edge) {
2+
const connects = new Array(n).fill().map((_) => []);
3+
for (const e of edge) {
4+
connects[e[0] - 1].push(e[1] - 1);
5+
connects[e[1] - 1].push(e[0] - 1);
6+
}
7+
8+
const visited = [1];
9+
const queue = [0];
10+
while (queue.length) {
11+
const cur = queue.shift();
12+
13+
for (const next of connects[cur]) {
14+
if (!visited[next]) {
15+
visited[next] = visited[cur] + 1;
16+
queue.push(next);
17+
}
18+
}
19+
}
20+
21+
const max = Math.max(...visited);
22+
23+
return visited.filter((el) => el === max).length;
24+
}
25+
26+
console.log(
27+
solution(6, [
28+
[3, 6],
29+
[4, 3],
30+
[3, 2],
31+
[1, 3],
32+
[1, 2],
33+
[2, 4],
34+
[5, 2],
35+
])
36+
);

0 commit comments

Comments
 (0)