如果在carList中有一個有序類型的免費汽車,該算法應檢查orderList中的每個訂單。檢查所有訂單的運行時間是什麼?
訂單包含您希望訂購的汽車類型以及取件和返回時間。現在我堅持查明這是否可以在O(nlog(n) + k)
中運行,其中k表示不同車型的數量。
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;
}
}
我測試了我的代碼,它似乎工作得很好。 我想過在汽車類型上使用散列函數,所以我只需要運行一個較小的列表(我的散列表中的每個列表只包含相同類型的汽車)。至少這是我的想法。
Here's問題陳述:
我有k個不同的汽車類型和n訂單 我需要檢查是否所有訂單進行處理,將要執行的意義there's沒有秩序無法。這隻能是由於沒有免費指定類型的汽車。訂單包含已經說過的理想汽車類型,取件和返回時間。
你會如何解決這個問題?
我假定每輛車型只有一個車可用?那麼問題可以重新表達爲:在每種車型的任何時刻,該車型至多有一個訂單。 – gogognome
訂單nlog(n)建議您使用quicksort或mergesort對某些內容進行排序。如果您在開始時間對訂單進行排序會怎樣? – gogognome
每輛車都有x輛車 – Sartharon