csct3434
[level 3] 네트워크 - 43162 본문
class Solution {
public int solution(int n, int[][] computers) {
int answer = 0;
boolean[] visited = new boolean[n];
for(int i=0; i<n; i++) {
if(!visited[i]) {
answer++;
dfs(i, n, computers, visited);
}
}
return answer;
}
private void dfs(int computer, int n, int[][] computers, boolean[] visited) {
visited[computer] = true;
for(int i=0; i<n; i++) {
if(computers[computer][i] == 1 && !visited[i]) {
dfs(i, n, computers, visited);
}
}
}
}
'프로그래머스' 카테고리의 다른 글
[level 2] 숫자 변환하기 - 154538 (0) | 2024.03.17 |
---|---|
[level 3] 단어 변환 - 43163 (0) | 2024.03.17 |
[level 3] 단속카메라 - 42884 (0) | 2024.03.17 |
[level 3] 섬 연결하기 - 42861 (0) | 2024.03.17 |
[level 3] 이중우선순위큐 - 42628 (0) | 2024.03.17 |