We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent cf258c3 commit c84e7deCopy full SHA for c84e7de
3주차/채수혁/레벨3__가장_먼_노드.js
@@ -0,0 +1,27 @@
1
+function solution(n, edge) {
2
+ const que = [[1, 0]];
3
+ const graph = {};
4
+ edge.forEach((v) => {
5
+ if (!graph[v[0]]) graph[v[0]] = [v[1]];
6
+ else graph[v[0]].push(v[1]);
7
+
8
+ if (!graph[v[1]]) graph[v[1]] = [v[0]];
9
+ else graph[v[1]].push(v[0]);
10
+ });
11
+ const visited = Array.from({ length: n + 1 }, () => false);
12
+ const cnts = Array.from({ length: n + 1 }, () => 0);
13
+ let index = 0;
14
+ visited[1] = true;
15
+ while (que.length > 0) {
16
+ const [node, cnt] = que.shift();
17
+ index = cnt;
18
+ cnts[cnt] += 1;
19
20
+ for (const next of graph[node]) {
21
+ if (visited[next]) continue;
22
+ que.push([next, cnt + 1]);
23
+ visited[next] = true;
24
+ }
25
26
+ return cnts[index];
27
+}
0 commit comments