i have solved this problem https://leetcode.com/problems/find-eventual-safe-states/submissions/
import scala.collection.mutable
object Solution {
def eventualSafeNodes(graph: Array[Array[Int]]): List[Int] = {
val seen = mutable.HashSet[Int]()
val safeNodes = mutable.ArrayBuffer[Int]()
val color_of = mutable.HashMap[Int, Int]().withDefaultValue(0)
def is_safe(node: Int): Boolean = {
color_of(node) = 1
for (neb <- graph(node)) {
if (color_of(neb) == 1) {
return false;
} else if (color_of(neb) == 0) {
if (!is_safe(neb)) {
return false;
}
}
}
color_of(node) = 2
return true
}
for (i <- 0 until graph.size) {
if (color_of(i) == 2) {
safeNodes.append(i)
} else if (color_of(i) == 0) {
if (is_safe(i)) {
safeNodes.append(i)
}
}
}
return safeNodes.toList
}
}
I would love some pointers on idiomatic code!