There is a line for the self-checkout machines at the supermarket. Your challenge is to write a function that calculates the total amount of time required for the rest of the customers to check out!
input
customers
: an array of positive integers representing the line. Each integer represents a customer, and its value is the amount of time they require to check out.
n
: a positive integer, the number of checkout tills.
rules
There is only one line serving many machines, and
The order of the line never changes, and
The front person in the line (i.e. the first element in the array/list) proceeds to a machine as soon as it becomes free.
output
The function should return an integer, the total time required.
examples
queueTime([5,3,4], 1) // should return 12 // because when there is 1 machine, the total time is just the sum of the times queueTime([10,2,3,3], 2) // should return 10 // because here n=2 and the 2nd, 3rd, and 4th people in the // queue finish before the 1st person has finished. queueTime([2,3,10], 2) // should return 12
Good luck, happy coding!
This challenge comes from mattlub on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (5)
Kotlin semi-functional solution:
This is a cool puzzle! Without going into spoilers, last year's Advent of Code had a similar puzzle, but with a few layers of complication on-top.
As an additional challenge, I added a record of the order shoppers finish.
Rust solution. This is easy when you use the right data structure - in this case, a heap.
Ruby solution: It's not the prettiest, but it works. I'm a Ruby newb so this was some good practice and I won't say no to any pointers or suggestions if I am doing something that's not very "Rubyish" (What word do they use for poorly used Ruby? In Python they say "that's not very "pythonic").