Skip to content

Instantly share code, notes, and snippets.

@qharlie
Last active January 26, 2018 04:03
Show Gist options
  • Save qharlie/7c004c6d3fa58bd0dbbf4bcdbe82dcf7 to your computer and use it in GitHub Desktop.
Save qharlie/7c004c6d3fa58bd0dbbf4bcdbe82dcf7 to your computer and use it in GitHub Desktop.
object Solution {
def buyTicket(seq: Array[Int]): Array[Int] = {
seq.drop(1) ++ seq.take(1).map(_ - 1 )
}
def waitingTime(ts : Array[Int], p : Int ) : Int = {
var tickets = ts; var time_in_line = 0; var position = p
while ( tickets(position) > 0 ) {
time_in_line += 1
tickets = buyTicket(tickets)
position = if ((position - 1 ) < 0 ) tickets.size - 1 else position - 1
}
time_in_line
}
def main (args : Array[String] ) : Unit =
{
println( waitingTime(List(5,5,2,3).toArray, 3) )
println( waitingTime(List(1,1,1,1).toArray, 0) )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment