-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.js
More file actions
58 lines (41 loc) · 842 Bytes
/
tempCodeRunnerFile.js
File metadata and controls
58 lines (41 loc) · 842 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const visited = Array(N + 1).fill(false);
function dfs(start, graph, depth, friend) {
if (depth >= 2) return friend;
for (let i = 0; i < graph[start].length; i++) {
let next = graph[start][i];
if (!visited[next]) {
visited[next] = true;
friend++;
friend = dfs(next, graph, depth + 1, friend);
}
}
return friend;
}
function solution(N, relation) {
var answer = [];
let graph = Array(N + 1)
.fill()
.map(() => []);
relation.forEach((el) => {
const [a, b] = el;
graph[a].push(b);
graph[b].push(a);
});
for (let i = 1; i <= N; i++) {
visited[i] = true;
let friendNum = dfs(i, graph, visited, 0, 0);
answer.push(friendNum);
for (let i = 1; j <= N; j++) {
visited[i] = false;
}
}
console.log(answer);
return answer;
}
solution(6, [
[1, 2],
[4, 2],
[1, 3],
[4, 5],
[2, 6],
]);