Skip to content

Commit 17578fe

Browse files
committed
[level 2] Title: 전력망을 둘로 나누기, Time: 9.08 ms, Memory: 86.5 MB -BaekjoonHub
1 parent 7db0a6c commit 17578fe

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
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-
메모리: 75 MB, 시간: 9.16 ms
7+
메모리: 86.5 MB, 시간: 9.08 ms
88

99
### 구분
1010

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

1717
### 제출 일자
1818

19-
2025년 09월 06일 19:55:10
19+
2025년 09월 16일 10:29:16
2020

2121
### 문제 설명
2222

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,44 @@
33
class Solution {
44
boolean[] visited;
55
public int solution(int n, int[][] wires) {
6+
67
visited = new boolean[n+1];
7-
int answer = n;
8-
int cnt = 1;
8+
int min = n;
9+
910
for(int[] wire : wires) {
1011
int towerA = wire[0];
1112
int towerB = wire[1];
12-
13-
// towerA와 연결된 타워의 갯수
13+
int cnt = 0;
14+
1415
visited[towerA] = true;
1516
visited[towerB] = true;
16-
cnt = dfs(towerA,wires);
17+
18+
cnt += dfs(towerA,wires);
19+
1720
visited[towerA] = false;
1821
visited[towerB] = false;
1922

20-
answer = Math.min(Math.abs((n - cnt) - cnt),answer);
23+
min = Math.min(min, Math.abs(n - cnt - cnt));
2124
}
22-
return answer;
25+
26+
return min;
2327
}
2428

25-
public int dfs(int towerA, int[][] wires){
26-
int cnt = 1;
29+
int dfs(int target, int[][] wires){
2730

28-
for(int i = 0; i < wires.length; i++) {
29-
int[] wire = wires[i];
31+
int cnt = 1;
32+
for(int[] wire : wires) {
33+
int towerA = wire[0];
34+
int towerB = wire[1];
3035

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;
36+
if(target == towerA && !visited[towerB]) {
37+
visited[towerB] = true;
38+
cnt += dfs(towerB,wires);
39+
visited[towerB] = false;
40+
} else if(target == towerB && !visited[towerA]){
41+
visited[towerA] = true;
42+
cnt += dfs(towerA,wires);
43+
visited[towerA] = false;
3944
}
4045
}
4146

0 commit comments

Comments
 (0)