This algorithm should check for each order in the orderList, if there´s a free car of the ordered type in the carList.
An order contains a type of an car you wish to order as well as pickup and return time. Now I´m stuck to find out if this can run in O(nlog(n) + k)
where k indicates the number of different car-types.
public static boolean CheckAllOrder(List orderList, List carList) {
int count = 0;
for (int i = 0; i < orderList.size(); i++) {
int type = orderList.get(i).type;
long start = orderList.get(i).start;
long end = orderList.get(i).end;
for (int j = 0; j < carList.size(); j++) {
Car currentCar = carList.get(j);
if (currentCar.type == type) {
if (currentCar.occTil == 0 && currentCar.occSince == 0) {
currentCar.occSince = start;
currentCar.occTil = end;
count++;
break;
} else if (currentCar.occTil < start) {
currentCar.occTil = end;
count++;
break;
} else if (currentCar.occSince > end) {
currentCar.occSince = start;
count++;
break;
}
}
}
}
if (count == orderList.size()) {
return true;
} else {
return false;
}
}
I did test my code and it seems to work just fine. I thought about using a hash-function over the car-types, so I only have to run through one smaller list (each list in my hash-table contains only cars of the same type). At least this is my idea.
Here´s the problem statement:
I do have k different car-types and n orders I need to check if all orders can be processed, meaning there´s no order unable to be performed. This can only be due to not having a car of the specified type free yet. An order contains as already said the desired car-type, pickup and return-time.
How would you solve this?