-
Notifications
You must be signed in to change notification settings - Fork 0
/
The_Supermarket_queue.cpp
48 lines (35 loc) · 1.97 KB
/
The_Supermarket_queue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
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:
queueTime(std::vector<int>{5,3,4}, 1)
// should return 12
// because when n=1, the total time is just the sum of the times
queueTime(std::vector<int>{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(std::vector<int>{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
*/
long queueTime(std::vector<int> customers,int n){
if (customers.empty()) return 0;
if (customers.size() < n) return *(std::max_element(customers.begin(), customers.end()));
int * pool = new int [n] {0};
for(std::vector<int>::iterator it = customers.begin(); it != customers.end(); ++it) {
*(std::min_element(pool, pool + n)) += *it;
}
int ret = *std::max_element(pool, pool + n);
delete [] pool;
return ret;
}