Problem Statement | |||||||||||||
BigBurger Inc. wants to see if having a single person at the counter both to take orders and to serve them is feasible. At each BigBurger, customers will arrive and get in line. When they get to the head of the line they will place their order, which will be assembled and served to them. Then they will leave the BigBurger and the next person in line will be able to order. We need to know how long a customer may be forced to wait before he or she can place an order. Given a script that lists each customer for a typical day, we want to calculate the maximum customer waiting time. Each customer in the script is characterized by an arrival time (measured in minutes after the store opened) and a service duration (the number of minutes between ordering and getting the food). Create a class BigBurger that contains method maxWait that is given a int[] arrival and a int[] service describing all the customers and returns the maximum time spent by a customer between arriving and placing the order. Corresponding elements ofarrival and service refer to the same customer, and they are given in the order in which they arrive at the store (arrival is in non-descending order). If multiple customers arrive at the same time they will all join the line at the same time, with the ones listed earlier ahead of ones appearing later in the list. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Constraints | |||||||||||||
- | arrival will contain between 1 and 50 elements inclusive | ||||||||||||
- | service will contain the same number of elements as arrival | ||||||||||||
- | the elements of arrival will be in non-decreasing order | ||||||||||||
- | each element of arrival will be between 1 and 720 inclusive | ||||||||||||
- | each element of service will be between 1 and 15 inclusive | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
这首题的思路是在每一轮循环中,求出当前这个顾客的等待时间。
根据(当前顾客的等待时间+当前顾客的Arrival时间+当前顾客的Service时间和下一个顾客的Arrival时间作比较,来计算出顾客的等待时间
public class BigBurger {
public int maxWait(int[] arrival, int[] service) {
int max = 0;
int customLen = arrival.length;
int waiTimes[] = new int[customLen];
if (customLen == 1)
return 0;
int i = 0;
int j = -1;
while (true) {
if (i == customLen)
break;
if (i == 0) {
waiTimes[i] = 0;
} else if (i > 0) {
if (waiTimes[i - 1] + arrival[i - 1] + service[j] <= arrival[i])
waiTimes[i] = 0;
else {
waiTimes[i] = waiTimes[i - 1] + arrival[i - 1] + service[j]
- arrival[i];
}
}
i++;
j++;
}
for (int c : waiTimes)
if (c > max)
max = c;
return max;
}
}