okay, here's my answer
May. 3rd, 2019 06:21 pmProblem: given a list of pairs of nonnegative integers, where pairs represent edges of a directed graph, check if the graph is a linear order.
Solutions:
Solutions:
def isLinear(src: List[(Int, Int)]): Boolean = {
val m = src.toMap.withDefaultValue(-1)
val tails = m.values.toSet
src.isEmpty || (src.size == m.size) && {
src find { case (a,_) => !tails(a) } match {
case Some((start,_)) =>
val visited =
Stream.iterate(start)(m).takeWhile(0 <=).take(src.size+2)
visited.length == src.size + 1
case None => false
}
}
}