Skip to content

Commit 120f8dc

Browse files
committed
[level 2] Title: 전력망을 둘로 나누기, Time: 9.16 ms, Memory: 75 MB -BaekjoonHub
1 parent 70eeb9f commit 120f8dc

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

프로그래머스/2/86971. 전력망을 둘로 나누기/README.md

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

55
### 성능 요약
66

7-
메모리: 74 MB, 시간: 8.99 ms
7+
메모리: 75 MB, 시간: 9.16 ms
88

99
### 구분
1010

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

1717
### 제출 일자
1818

19-
2025년 09월 05일 11:44:16
19+
2025년 09월 06일 19:55:10
2020

2121
### 문제 설명
2222

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
import java.util.*;
2+
23
class Solution {
34
boolean[] visited;
4-
int answer;
55
public int solution(int n, int[][] wires) {
6-
answer = n;
76
visited = new boolean[n+1];
8-
9-
for(int[] wire : wires){
7+
int answer = n;
8+
int cnt = 1;
9+
for(int[] wire : wires) {
1010
int towerA = wire[0];
1111
int towerB = wire[1];
12-
visited[towerB] = true;
12+
13+
// towerA와 연결된 타워의 갯수
1314
visited[towerA] = true;
14-
int cnt = dfs(towerA,wires,n); // towerA와 연결된 타워만 찾으면 됨
15+
visited[towerB] = true;
16+
cnt = dfs(towerA,wires);
1517
visited[towerA] = false;
1618
visited[towerB] = false;
1719

18-
answer = Math.min(Math.abs(n - (2 * cnt)), answer);
20+
answer = Math.min(Math.abs((n - cnt) - cnt),answer);
1921
}
20-
2122
return answer;
2223
}
2324

24-
int dfs(int towerA, int[][] wires, int n){
25+
public int dfs(int towerA, int[][] wires){
2526
int cnt = 1;
26-
for(int[] wire : wires){
27+
28+
for(int i = 0; i < wires.length; i++) {
29+
int[] wire = wires[i];
2730

28-
// 연결된 송전탑 찾음(방문한적 x)
29-
if(wire[0] == towerA && !visited[wire[1]]) {
30-
visited[wire[1]] = true; // 방문 처리
31-
cnt += dfs(wire[1],wires,n); // dfs
32-
visited[wire[1]] = false; // 백트래킹
33-
34-
} else if(wire[1] == towerA && !visited[wire[0]]) {
35-
visited[wire[0]] = true; // 방문 처리
36-
cnt += dfs(wire[0],wires, n); // dfs
37-
visited[wire[0]] = false; // 백트래킹
31+
if(towerA == wire[0] && !visited[wire[1]]){
32+
visited[wire[1]] = true;
33+
cnt += dfs(wire[1], wires);
34+
visited[wire[1]] = false;
35+
} else if(towerA == wire[1] && !visited[wire[0]]){
36+
visited[wire[0]] = true;
37+
cnt += dfs(wire[0], wires);
38+
visited[wire[0]] = false;
3839
}
3940
}
4041

4142
return cnt;
4243
}
43-
4444
}

0 commit comments

Comments
 (0)