[level 3] 네트워크 - 43162

2024. 9. 27. 17:59·코딩테스트/프로그래머스

[level 3] 네트워크 - 43162

문제 링크

성능 요약

메모리: 79.7 MB, 시간: 0.27 ms

구분

코딩테스트 연습 > 깊이/너비 우선 탐색(DFS/BFS)

채점결과

정확성: 100.0
합계: 100.0 / 100.0

제출 일자

2024년 09월 27일 17:45:28

문제 설명

네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있을 때 컴퓨터 A와 컴퓨터 C도 간접적으로 연결되어 정보를 교환할 수 있습니다. 따라서 컴퓨터 A, B, C는 모두 같은 네트워크 상에 있다고 할 수 있습니다.

컴퓨터의 개수 n, 연결에 대한 정보가 담긴 2차원 배열 computers가 매개변수로 주어질 때, 네트워크의 개수를 return 하도록 solution 함수를 작성하시오.

제한사항
  • 컴퓨터의 개수 n은 1 이상 200 이하인 자연수입니다.
  • 각 컴퓨터는 0부터 n-1인 정수로 표현합니다.
  • i번 컴퓨터와 j번 컴퓨터가 연결되어 있으면 computers[i][j]를 1로 표현합니다.
  • computer[i][i]는 항상 1입니다.
입출력 예
n computers return
3 [[1, 1, 0], [1, 1, 0], [0, 0, 1]] 2
3 [[1, 1, 0], [1, 1, 1], [0, 1, 1]] 1
입출력 예 설명

예제 #1
아래와 같이 2개의 네트워크가 있습니다.

image0.png

예제 #2
아래와 같이 1개의 네트워크가 있습니다.

image1.png

해결 답안

class Solution {
    boolean[] visited;
    int[][] map;
    public int solution(int n, int[][] computers) {
        int answer = 0;
        visited = new boolean[n];
        map = computers;
        
        for(int i=0; i<n; i++){
        	// 0번 인덱스부터 이어져 있는 모든 노드 순회
            if(!visited[i]){
                dfs(i);
                // 방문하지 않은 새로운 노드에서만 answer가 증가하도록 선언
                answer++;
            }
        }
        
        return answer;
    }
    
    void dfs(int idx){
        visited[idx] = true;
        for(int i=0; i<map.length; i++){
        	// 다음 방문할 노드 조건
            // 1. 자기 자신이 아님
            // 2. 방문하지 않은 곳에만 방문함
            // 3. 자신과 이어져있는 노드여야 함
            if(i!=idx && !visited[i] && map[idx][i] == 1){
                dfs(i);
            }
        }
    }
}

 

주석 제거 코드

더보기
더보기
class Solution {
    boolean[] visited;
    int[][] map;
    public int solution(int n, int[][] computers) {
        int answer = 0;
        visited = new boolean[n];
        map = computers;
        
        for(int i=0; i<n; i++){
            if(!visited[i]){
                dfs(i);
                answer++;
            }
        }
        
        return answer;
    }
    
    void dfs(int idx){
        visited[idx] = true;
        for(int i=0; i<map.length; i++){
            if(i!=idx && !visited[i] && map[idx][i] == 1){
                dfs(i);
            }
        }
    }
}

 

'코딩테스트 > 프로그래머스' 카테고리의 다른 글

[level 3] 단어 변환 - 43163  (0) 2024.09.30
[level 2] 게임 맵 최단거리 - 1844  (8) 2024.09.29
[level 2] [3차] n진수 게임 - 17687  (0) 2024.09.26
[level 2] 튜플 - 64065  (0) 2024.09.22
[level 2] 2 x n 타일링 - 12900  (0) 2024.09.22
'코딩테스트/프로그래머스' 카테고리의 다른 글
  • [level 3] 단어 변환 - 43163
  • [level 2] 게임 맵 최단거리 - 1844
  • [level 2] [3차] n진수 게임 - 17687
  • [level 2] 튜플 - 64065
mint723dev
mint723dev
mint723dev 님의 블로그 입니다.
  • mint723dev
    mint723dev 님의 블로그
    mint723dev
  • 전체
    오늘
    어제
    • 분류 전체보기 (88)
      • Computer Science (16)
        • Computer Architecture (0)
        • Data Structure (2)
        • Database (4)
        • Network (4)
        • Operating System (6)
        • Software Engineering (0)
      • Java (6)
      • 자료 구조 (0)
      • 코딩테스트 (62)
        • LeetCode (1)
        • 프로그래머스 (29)
        • 백준 (32)
      • Spring (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    오블완
    DFS
    JVM
    탐욕법
    DP
    티스토리챌린지
    deque
    BFS
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
mint723dev
[level 3] 네트워크 - 43162
상단으로

티스토리툴바