-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path가장 먼 노드.swift
More file actions
49 lines (40 loc) · 1.11 KB
/
가장 먼 노드.swift
File metadata and controls
49 lines (40 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// 가장 먼 노드.swift
//
//
// Created by chihoooon on 2022/01/26.
//
import Foundation
func bfs(_ start: Int, graph: [[Int]], dist: [Int], visit: [Bool]) -> [Int] {
var queue: [Int] = []
var dist = dist
var visit = visit
queue.append(start)
visit[start] = true
while !queue.isEmpty {
let src = queue.first!
queue.removeFirst()
graph[src].forEach {
if !visit[$0] {
queue.append($0)
visit[$0] = true
dist[$0] = dist[src] + 1
}
}
}
return dist
}
func solution(_ n:Int, _ edge:[[Int]]) -> Int {
let dist = Array(repeating: 0, count: n)
let visit = Array(repeating: false, count: n)
var graph: [[Int]] = Array(repeating: [], count: n)
edge.forEach {
let x = $0[0] - 1
let y = $0[1] - 1
graph[x].append(y)
graph[y].append(x)
}
let distanceArr = bfs(0, graph: graph, dist: dist, visit: visit)
let maxNode = distanceArr.max()!
return distanceArr.filter { $0 == maxNode }.count
}