- 난이도: Lv3
프로그래머스 링크: https://school.programmers.co.kr/learn/courses/30/lessons/43162
풀이 링크(GitHub): hayannn/CodingTest_Java/프로그래머스/2/네트워크
풀이 시간 : 35분
class Solution {
public int solution(int n, int[][] computers) {
int networkCount = 0;
boolean[] visitedNodes = new boolean[n];
for (int i = 0; i < n; i++) {
if (!visitedNodes[i]) {
networkCount++;
exploreNetwork(i, visitedNodes, computers);
}
}
return networkCount;
}
private void exploreNetwork(int currentNode, boolean[] visitedNodes, int[][] computers) {
visitedNodes[currentNode] = true;
for (int nextNode = 0; nextNode < computers.length; nextNode++) {
if (!visitedNodes[nextNode] && computers[currentNode][nextNode] == 1) {
exploreNetwork(nextNode, visitedNodes, computers);
}
}
}
}