Skip to content

Commit 57044ec

Browse files
committed
[level 3] Title: 네트워크, Time: 0.93 ms, Memory: 86.1 MB -BaekjoonHub
1 parent 66f2ce5 commit 57044ec

File tree

2 files changed

+33
-43
lines changed

2 files changed

+33
-43
lines changed

프로그래머스/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-
메모리: 78.9 MB, 시간: 0.50 ms
7+
메모리: 86.1 MB, 시간: 0.93 ms
88

99
### 구분
1010

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

1717
### 제출 일자
1818

19-
2024년 03월 12일 13:54:38
19+
2025년 11월 10일 20:38:32
2020

2121
### 문제 설명
2222

프로그래머스/3/43162. 네트워크/네트워크.java

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,49 @@
11
import java.util.*;
2+
23
class Solution {
3-
static boolean[] visited;
4-
static ArrayList<Integer>[] A;
5-
static int max;
4+
5+
static ArrayList<Integer> list[];
6+
static boolean visited[];
67
public int solution(int n, int[][] computers) {
78
int answer = 0;
8-
max =1;
9-
visited = new boolean[n+1];
10-
11-
A = new ArrayList[n+1];
12-
13-
for(int i =1; i<=n; i++)
14-
{
15-
A[i] = new ArrayList<Integer>();
9+
10+
list = new ArrayList[n+1];
11+
12+
for(int i =1; i<=n; i++) {
13+
list[i] = new ArrayList<Integer>();
1614
}
17-
18-
for (int i = 0; i < n; i++) {
19-
for (int j = 0; j < n; j++) {
20-
if (computers[i][j] == 1) { // If there's a connection between i and j
21-
A[i+1].add(j+1); // Adjust indices because i and j are zero-based
15+
visited = new boolean[n+1];
16+
for(int i =0; i<computers.length; i++) {
17+
for(int j =0; j<computers[i].length;j++) {
18+
if(computers[i][j] == 1) {
19+
list[i+1].add(j+1);
20+
list[j+1].add(i+1);
2221
}
2322
}
2423
}
2524

26-
for(int i =1; i<=n; i++)
27-
{
28-
if(visited[i]==false)
29-
{
30-
dfs(i);
31-
answer++;
32-
25+
for(int i =1; i<=n; i++) {
26+
if(!visited[i]){
27+
bfs(i);
28+
answer+=1;
3329
}
3430
}
31+
3532
return answer;
3633
}
37-
static void dfs(int value)
38-
{
39-
if(visited[value])
40-
return;
41-
34+
static void bfs(int start) {
35+
4236
Queue<Integer> queue = new LinkedList<>();
37+
38+
queue.add(start);
4339

44-
visited[value]=true;
45-
queue.add(value);
46-
47-
while(!queue.isEmpty())
48-
{
49-
int now =queue.poll();
50-
max=now;
51-
for(int i : A[now])
52-
{
53-
if(visited[i]==false)
54-
{
55-
visited[i]=true;
56-
queue.add(i);
40+
while(!queue.isEmpty()) {
41+
int now = queue.poll();
42+
43+
for(int cur : list[now]) {
44+
if(!visited[cur]) {
45+
queue.add(cur);
46+
visited[cur] = true;
5747
}
5848
}
5949
}

0 commit comments

Comments
 (0)