Last active
December 21, 2019 18:30
-
-
Save go-cristian/adc879e79b7340c00785067585b06b8c to your computer and use it in GitHub Desktop.
main
This file contains hidden or 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
fun getTimes(times: Array<Int>, directions: Array<Int>): Array<Int> { | |
val result: Array<Int> = Array(times.size) { -1 } | |
val endTime: Int = times.max() ?: 0 | |
if (endTime == 0) return result | |
val inQueue: Queue<Int> = LinkedList() | |
val outQueue: Queue<Int> = LinkedList() | |
var currentIndex = 0 | |
var turnstile = 0 | |
for (time in 0 until endTime + 1) { | |
while (times[currentIndex] == time) { | |
if (directions[currentIndex] == 0) { | |
inQueue.add(currentIndex) | |
} | |
if (directions[currentIndex] == 1) { | |
outQueue.add(currentIndex) | |
} | |
currentIndex++ | |
if (currentIndex == times.size) break | |
} | |
val noPersonOnTurn = inQueue.size + outQueue.size == 0 | |
if (noPersonOnTurn) turnstile = 0 | |
if (turnstile == 1 || outQueue.size == 0) { | |
if (inQueue.size > 0) { | |
val next = inQueue.poll() | |
result[next] = time | |
turnstile = 1 | |
} | |
} else { | |
if (outQueue.size > 0) { | |
val next = outQueue.poll() | |
result[next] = time | |
turnstile = 2 | |
} | |
} | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment