Skip to content

Commit 3f978af

Browse files
committed
[level 3] Title: 네트워크, Time: 0.37 ms, Memory: 43.6 MB -BaekjoonHub
1 parent cccf077 commit 3f978af

2 files changed

Lines changed: 27 additions & 12 deletions

File tree

‎프로그래머스/3/43162. 네트워크/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### 성능 요약
66

7-
메모리: 33.6 MB, 시간: 0.66 ms
7+
메모리: 43.6 MB, 시간: 0.37 ms
88

99
### 구분
1010

@@ -16,7 +16,7 @@
1616

1717
### 제출 일자
1818

19-
2026년 05월 05일 14:30:55
19+
2026년 10월 26일 04:02:47
2020

2121
### 문제 설명
2222

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
1-
function dfs(node, n, computers, visited) {
2-
visited[node] = true;
1+
/**
2+
* A-B, B-C 연결되어있으면 A-C도 간접연결
3+
* ==> A,B,C는 모두 같은 네트워크 상에 존재
4+
*
5+
* 네트워크의 개수 return
6+
* i번, j번 컴퓨터 연결되어있으면 computers[i][j]=1
7+
* computers[i][i] 항상 1
8+
**/
39

4-
for (let i = 0; i < n; i++) {
5-
if (i !== node && computers[node][i] === 1 && !visited[i]) {
6-
dfs(i, n, computers, visited);
10+
function bfs(computers, n, start, visited) {
11+
const queue = [start];
12+
let head = 0;
13+
visited[start] = true;
14+
15+
while (head < queue.length) {
16+
const current = queue[head++];
17+
18+
for (let i = 0; i < n; i++) {
19+
if (computers[current][i] !== 1) continue; //연결안됨
20+
if (visited[i]) continue; //이미 방문함
21+
visited[i] = true; //방문처리
22+
queue.push(i);
723
}
824
}
925
}
1026

1127
function solution(n, computers) {
1228
let answer = 0;
13-
const visited = new Array(n).fill(false);
29+
const visited = Array.from({ length: n }, () => false);
1430

1531
for (let i = 0; i < n; i++) {
16-
if (!visited[i]) {
17-
dfs(i, n, computers, visited);
18-
answer++;
19-
}
32+
if (visited[i]) continue;
33+
answer++; //방문하지않는 컴퓨터가 나오면 새 네트워크
34+
bfs(computers, n, i, visited); //방문처리
2035
}
2136
return answer;
2237
}

0 commit comments

Comments
 (0)