We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Just wanted to expand on this a bit since it would help me clarify my own thoughts!
3
0 3
1 9
2 6
Base Case:
The first pizza will always be made and cooked first, finishing at time of 3
At time of 3, Customer 1 and 2 have arrived.
At this point, since 3 > 1 and 3 > 2, this is where the decision is made.
We choose customer 2, since 6 < 9
Once customer 2 is finished, the current time is now
3+(3-2 + 6) -> 3+7
3 is curr time, and we subtract 2 from it since customer 2 arrived at 2, so rather than adding just 6 (the time to cook customer 2's pizza), we need to add 3-2 which is 1. If customer 2 arrived at time 3, then we could have just added 3+6.
Then add the last value since it is the last customer
3+7+(9+9-1)
Lets define Minimum Average Waiting Time as MAWT.
Assumptions:
There are 2 time sources we need to keep track of, the incremental time representing arrivals (i.e. 0,1,2), and the current time done in strides (arrival time + time to cook)
The incremental time representing arrivals should be used to check IF we can add a customer to the waiting queue (since we CANNOT consider a pizza IF the customer hasnt even arrived yet!!!)
IF the waiting queue is NOT EMPTY, pop the top and add its value to the CURRENT TIME DONE IN STRIDES (MAWT = current time + time to cook - time arrived)
The finish case is if the waiting list is empty AND the incremental time representing arrivals is equal to n(number of customers that come in)
NOTE:
Current time and MAWT are NOT the same thing.
Current time is the time to cook times added together (3+6+9)
MAWT is the current time WITH the waiting time added to it
(3 + (6+3-2) + (9+9-1))
Hope this helps, and happy coding :3
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Average Waiting Time
You are viewing a single comment's thread. Return to all comments →
Just wanted to expand on this a bit since it would help me clarify my own thoughts!
3 0 3 1 9 2 6
Base Case:
Once customer 2 is finished, the current time is now 3+(3-2 + 6) -> 3+7 3 is curr time, and we subtract 2 from it since customer 2 arrived at 2, so rather than adding just 6 (the time to cook customer 2's pizza), we need to add 3-2 which is 1. If customer 2 arrived at time 3, then we could have just added 3+6.
Then add the last value since it is the last customer 3+7+(9+9-1)
Lets define Minimum Average Waiting Time as MAWT.
Assumptions:
There are 2 time sources we need to keep track of, the incremental time representing arrivals (i.e. 0,1,2), and the current time done in strides (arrival time + time to cook)
NOTE:
Hope this helps, and happy coding :3