Skip to content

Commit 5054eea

Browse files
authored
[20250905] PGM / LV3 / 섬 연결하기 / 김수연
[20250905] PGM / LV3 / 섬 연결하기 / 김수연
2 parents a8b9317 + a291dfd commit 5054eea

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
```java
2+
import java.util.*;
3+
class Solution {
4+
public int solution(int n, int[][] costs) {
5+
int answer = 0;
6+
HashSet<Integer> set = new HashSet<>();
7+
Arrays.sort(costs, (o1, o2) -> o1[2] - o2[2]);
8+
set.add(costs[0][0]);
9+
while (set.size() < n) {
10+
for (int[] cost : costs) {
11+
if (set.contains(cost[0]) && set.contains(cost[1])) continue;
12+
else if (set.contains(cost[0]) || set.contains(cost[1])) {
13+
set.add(cost[0]);
14+
set.add(cost[1]);
15+
answer += cost[2];
16+
break;
17+
}
18+
}
19+
}
20+
return answer;
21+
}
22+
}
23+
```

0 commit comments

Comments
 (0)