There is a queue for the self-checkout tills at the supermarket. Your task is write a function to calculate the total time required for all the customers to check out!
The function has two input variables:
customers: an array (list in python) of positive integers representing the queue. 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.
The function should return an integer, the total time required.
EDIT: A lot of people have been confused in the comments. To try to prevent any more confusion:
There is only ONE queue, and
The order of the queue NEVER changes, and
Assume that the front person in the queue (i.e. the first element in the array/list) proceeds to a till as soon as it becomes free.
The diagram on the wiki page I linked to at the bottom of the description may be useful.
So, for example:
queue_time([5,3,4], 1)
# should return 12
# because when n=1, the total time is just the sum of the times
queue_time([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.
queue_time([2,3,10], 2)
# should return 12
N.B. You should assume that all the test input will be valid, as specified above.
P.S. The situation in this kata can be likened to the more-computer-science-related idea of a thread pool, with relation to running multiple processes at the same time: https://en.wikipedia.org/wiki/Thread_pool
Test case:
queue_time([2,3,10], 2) should be 12
queue_time([], 5) should be 0
queue_time([2], 5) should be 2
queue_time([1,2,3,4,5], 100) should be 5
queue_time([2,3,10,2,3], 2) should be 12
题目大意
超市购物收银排队问题:一个队伍customers[],队伍中每个元素是当前客户处理花费的时长。n是指有n个购物台,可以同时处理n个客户。主要就是求整个队伍处理花费的总时长。
队伍是按顺序处理的,第一个处理完毕以后会继续处理之后的数据。
比如给出的例子:
queue_time([2,3,10], 2) should be 12
两台处理器,一个处理第一个时间2,一个处理第二个时间3。处理完第一个的机器直接继续处理第三个,而此时处理第二个元素的机器还在处理。所以总时间2+10=12
我的解法
TIM图片20180717224540.png创建长度为n的空数组,通过for循环:将需要处理的队伍customers的元素加上该新数组queue赋值给queue的第一个元素,将该数组按升序排列。
queue_time([2,3,10], 2) should be 12
一开始queue=[0,0]
sort
queue=[0,0]
+=
queue=[2,0]
sort后
queue=[0,2]
+=
queue=[3,2,]
sort
queue=[2,3]
+=
queue=[12,2,3]
sort
queue=[2,3,12]
返回最大值12
网友评论