-
문제
https://programmers.co.kr/learn/courses/30/lessons/43162
How to Solve ?
[1] DFS
function DFS(index, visited, computers) { visited[index] = true; for (let i = 0; i < computers.length; i++) { if ((computers[index][i] === 1) & !visited[i]) { DFS(i, visited, computers); } } } function solution(n, computers) { let answer = 0; const visited = []; for (const computer of computers) { visited.push(false); } for (let i = 0; i < computers.length; i++) { if (!visited[i]) { DFS(i, visited, computers); answer += 1; } } return answer; } let n = 3; let computers = [ [1, 1, 0], [1, 1, 0], [0, 0, 1], ]; console.log(solution(n, computers));
'PS > 프로그래머스' 카테고리의 다른 글
[프로그래머스_Lv2] H-index ( by using JavaScript ) (0) 2021.05.25 [ 프로그래머스_Lv3 ] 베스트앨범 ( by using JavaScript ) (0) 2021.05.25 [프로그래머스_Lv1] 약수의 개수와 덧셈 ( by using JavaScript ) (0) 2021.05.23 [프로그래머스_Lv2 ] 메뉴 리뉴얼 ( by using JavaScript ) (0) 2021.05.23 [프로그래머스_Lv2] 예상 대진표 ( by using JavaScript ) (0) 2021.05.21 댓글