🙂

2021/11/29に公開

Question

``````There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [ui, vi] denotes a bi-directional edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.

You want to determine if there is a valid path that exists from vertex start to vertex end.

Given edges and the integers n, start, and end, return true if there is a valid path from start to end, or false otherwise.
``````

• n個の頂点を持つ双方向グラフがあり，各頂点には0からn - 1（含む）までのラベルが付けられている
• グラフの辺は2次元整数配列edgeで表され，各edge[i] = [ui, vi]は頂点uiと頂点viの間の双方向の辺を示す
• すべての頂点のペアは最大で1つのエッジで接続されており、どの頂点も自分自身へのエッジを持っていない

Code

``````class Solution {
fun validPath(n: Int, edges: Array<IntArray>, start: Int, end: Int): Boolean {
var dests = mutableMapOf<Int, MutableList<Int>>()
var visited = mutableSetOf<Int>()

for (edge in edges) {
val from = edge[0]
val to = edge[1]

dests[from]?.let {
} ?: run {
dests[from] = mutableListOf(to)
}
dests[to]?.let {
} ?: run {
dests[to] = mutableListOf(from)
}
}

fun dfs(node: Int): Boolean {
if (end == node) return true
for (child in dests[node]!!) {
if (child !in visited && dfs(child)) {
return true
}
}
return false
}

return dfs(start)
}
}
``````

Profile

• Runtime: 1900 ms
• Memory Usage: 336.6 MB

GitHubで編集を提案