Untitled
unknown
plain_text
a month ago
1.9 kB
3
Indexable
Never
int calculateLowestCost(const vector<Pizza>& menu, const vector<OrderItem>& orders) { vector<int> itemPrices; vector<int> upgradeCosts; unordered_map<string, vector<int>> orderTally; auto getSizeIndex = [](const string& size) { return (size == "Small") ? 0 : (size == "Medium") ? 1 : 2; }; unordered_map<string, vector<int>> menuPrices; for (const auto& pizza : menu) { menuPrices[pizza.name] = {pizza.price_S, pizza.price_M, pizza.price_L}; } int totalCost = 0; for (const auto& item : orders) { int sizeIdx = getSizeIndex(item.size); orderTally[item.name].resize(3, 0); orderTally[item.name][sizeIdx] += item.quantity; int itemPrice = menuPrices[item.name][sizeIdx]; totalCost += itemPrice * item.quantity; for (int i = 0; i < item.quantity; ++i) { itemPrices.push_back(itemPrice); if (sizeIdx == 2) { upgradeCosts.push_back(itemPrice - menuPrices[item.name][1]); } } } sort(itemPrices.rbegin(), itemPrices.rend()); sort(upgradeCosts.rbegin(), upgradeCosts.rend()); int freeItemDiscount = (itemPrices.size() >= 3) ? itemPrices[2] : 0; int bulkDiscount = 0; if (itemPrices.size() >= 5) { bulkDiscount = accumulate(itemPrices.begin(), itemPrices.begin() + 5, 0) - 100; } int comboDiscount = 0; for (const auto& entry : orderTally) { const auto& counts = entry.second; comboDiscount += menuPrices[entry.first][0] * min(counts[0], counts[2]); } int upgradeDiscount = 0; if (upgradeCosts.size() >= 3) { upgradeDiscount = accumulate(upgradeCosts.begin(), upgradeCosts.begin() + 3, 0); } int maxDiscount = max({freeItemDiscount, bulkDiscount, comboDiscount, upgradeDiscount}); return totalCost - maxDiscount; }
Leave a Comment