[Link] https://programmers.co.kr/learn/courses/30/lessons/43162
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function solution(n, computers) {
let cnt = 0;
const visit = new Array(computers.length).fill(false);
for (let i = 0; i < n; i++) {
if (!visit[i]) {
cnt++;
dfs(computers, visit, i, n);
}
}
return cnt;
}
function dfs(computers, visit, idx, len) {
visit[idx] = true;
for (let i = 0; i < len; i++) {
if (computers[idx][i] == 1 && !visit[i] && i != idx)
dfs(computers, visit, i, len);
}
}