Skip to content

Commit 3e2e1c2

Browse files
committed
Add 섬 연결하기
1 parent fcf7753 commit 3e2e1c2

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Foundation
2+
3+
func solution(_ n:Int, _ costs:[[Int]]) -> Int {
4+
var costs = costs.sorted{ $0[2] < $1[2] }
5+
var visited = Array(repeating: false, count: n)
6+
var answer = 0
7+
8+
visited[costs[0][0]] = true
9+
10+
while visited.contains(false) {
11+
for cost in costs {
12+
if visited[cost[0]] && !visited[cost[1]] {
13+
visited[cost[1]] = true
14+
answer += cost[2]
15+
break
16+
} else if !visited[cost[0]] && visited[cost[1]] {
17+
visited[cost[0]] = true
18+
answer += cost[2]
19+
break
20+
}
21+
}
22+
}
23+
24+
return answer
25+
}

0 commit comments

Comments
 (0)