Created
March 26, 2019 17:18
-
-
Save ccw/bafef76e5642610e94deeb64b63cd1e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Turnstile { | |
def getTimes(time: Array[Int], direction: Array[Int]): Array[Int] = { | |
var lt = -1 | |
var ld = 1 | |
val result = Array.fill(time.length) { 0 } | |
time.foldLeft(time.zipWithIndex) { (q, _) => | |
val nt = q.head._1 | |
val (taken, dir) = if (nt - lt > 1) { | |
// no previous second usage | |
( | |
q.takeWhile(v => v._1 == nt), | |
1 | |
) | |
} else { | |
( | |
q.takeWhile(v => v._1 - lt <= 1), | |
ld | |
) | |
} | |
val next = taken.find(v => direction(v._2) == dir).getOrElse(taken.head) | |
lt = if (next._1 > lt + 1) next._1 else lt + 1 | |
ld = direction(next._2) | |
result(next._2) = lt | |
taken.filter(_ != next) ++ q.drop(taken.length) | |
} | |
result | |
} | |
def main(args: Array[String]): Unit = { | |
val result = getTimes(Array(1, 1, 1, 4, 4, 4, 7, 7, 7), Array(0, 1, 0, 1, 1, 0, 1, 1, 0)) | |
println(result.toList) | |
println(result equals Array(1)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment