A small neighborhood grocery store tracks how many customers are waiting in each of its checkout lanes. The store uses the class GroceryStore, whose skeleton is shown below.
public class GroceryStore
{
/** The number of customers currently waiting in each lane.
* laneQueues[k] stores the customers in lane k (0-based).
* The array is never null and has at least one element. */
private int[] laneQueues;
/** The number of seconds it takes to serve one customer
* in any lane. Always positive. */
private int secondsPerCustomer;
/** Constructs a GroceryStore with the given queue snapshot and
* per-customer service time. */
public GroceryStore(int[] queues, int secs)
{
/* not shown */;
}
/** Returns the estimated wait time, in seconds, for a customer
* who joins the back of lane laneIndex right now. Part (a) */
public int estimatedWait(int laneIndex)
{
/* part (a) */;
}
/** Returns the index of the lane with the shortest estimated wait.
* If multiple lanes are tied, the lowest such index is returned.
* Part (b) */
public int fastestLane()
{
/* part (b) */;
}
// Other methods and constructors are not shown.
}
Part A
Write the method estimatedWait. A customer joining the back of a lane must wait for every customer already in that lane to be served plus the time to be served themselves. Each customer takes exactly secondsPerCustomer seconds.
For example, suppose secondsPerCustomer is 45 and laneQueues is {3, 1, 4, 2}. The following table shows the expected return values.
| Call |
Returned Value |
Explanation |
estimatedWait(0) |
180 |
3 ahead + 1 self = 4 × 45 |
estimatedWait(1) |
90 |
1 ahead + 1 self = 2 × 45 |
estimatedWait(2) |
225 |
4 ahead + 1 self = 5 × 45 |
estimatedWait(3) |
135 |
2 ahead + 1 self = 3 × 45 |
Assume laneIndex is a valid index in laneQueues.
Part B
Write the method fastestLane. It returns the index of the lane with the shortest estimated wait for a new customer joining now. If two or more lanes are tied for the minimum, return the smallest such index.
For example, if secondsPerCustomer is 30 and laneQueues is {2, 0, 2, 0, 3}, then the estimated waits are {90, 30, 90, 30, 120}. The minimum is 30, achieved by lanes 1 and 3, so fastestLane() returns 1.
You must call estimatedWait appropriately to receive full credit. Assume estimatedWait works as specified regardless of what you wrote in Part A.