Skip to content

Commit 0e72282

Browse files
authored
Update 1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero.kt
1 parent 2cd1fde commit 0e72282

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

Diff for: kotlin/1466-reorder-routes-to-make-all-paths-lead-to-the-city-zero.kt

+42-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,47 @@
1-
package kotlin
1+
/*
2+
* DFS Solution
3+
*/
4+
class Solution {
5+
fun minReorder(n: Int, connections: Array<IntArray>): Int {
6+
7+
val neighbors = ArrayList<ArrayList<Int>>().apply {
8+
for (i in 0 until n)
9+
this.add(ArrayList<Int>())
10+
for ((u,v) in connections) {
11+
this.get(u).apply { this.add(v) }
12+
this.get(v).apply { this.add(u) }
13+
}
14+
}
15+
16+
val edges = HashSet<String>().apply {
17+
for ((u,v) in connections)
18+
this.add("$u:$v")
19+
}
220

3-
import java.util.LinkedList
21+
var changes = 0
22+
val visited = BooleanArray(n)
23+
24+
fun dfs(n: Int) {
25+
for (nei in neighbors[n]) {
26+
if (visited[nei] == true)
27+
continue
28+
if ("$nei:$n" !in edges)
29+
changes++
30+
visited[nei] = true
31+
dfs(nei)
32+
}
33+
}
34+
35+
visited[0] = true
36+
dfs(0)
37+
38+
return changes
39+
}
40+
}
441

42+
/*
43+
* BFS Solution
44+
*/
545
class Solution {
646
fun minReorder(n: Int, connections: Array<IntArray>): Int {
747
// node -> (adjacentNode,isArtificialEdge)

0 commit comments

Comments
 (0)