본문 바로가기

코딩테스트 문제풀이

[프로그래머스, javascript] 네트워크

 

function solution(n, computers) {
  var visited = new Array(n).fill(false);

  function visit(q, arr){
    if(!q.length) return;
    const val = q.pop();
    visited[val] = true;

    arr[val].map((v,idx)=>{
      if(!!v && !q.includes(idx) && !visited[idx]){
        q.push(idx)
      }
    })

    return visit(q, arr);
  }

  var network = 0;
  visited.map((v,i)=>{
    if(!v){
      //방문 안했을 경우?
      network++;
      visit([i], computers);
    };
  });
  return network;
}

programmers.co.kr/learn/courses/30/lessons/43162

 

코딩테스트 연습 - 네트워크

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

programmers.co.kr