-
728x90
- 1~n개의 네트워크에 대해 탐색한다. => dfs
- 만약 연결된 네트워크중에 탐색하지 않은게 있다면, 계속해서 타고 들어가 탐색을 계속한다.
class Solution {static boolean[] isVisited;public int solution(int n, int[][] computers) {int answer = 0;isVisited = new boolean[n]; // 방문 처리for(int i = 0; i < n; i++) {if(!isVisited[i]) {dfs(i, computers);answer++;}}return answer;}static void dfs(int cur, int[][] computers) {isVisited[cur] = true;for(int j = 0; j < computers.length; j++) {if(!isVisited[j] && computers[cur][j] == 1) {dfs(j, computers);}}}}데롱디롱희희.. (๑′ᴗ‵๑)
'알고리즘 풀이 > 프로그래머스' 카테고리의 다른 글
[level3] 프로그래머스 - 여행경로(JAVA) (0) 2021.09.14 [level3] 프로그래머스 - 단어 변환(JAVA) (0) 2021.09.14 [level2] 프로그래머스 - 타겟 넘버(JAVA) (0) 2021.09.14 [level2] 프로그래머스 - 124 나라의 숫자 (2) 2021.09.12 [level2] 프로그래머스 - 순위 검색(JAVA) (0) 2021.09.12