Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions 2353. Design a Food Rating System 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class FoodRatings {
unordered_map<string, string> food_cuisines;
unordered_map<string, int> food_ratings;
struct Node {
int rating;
string food;
};
struct Cmp {
bool operator()(const Node& a, const Node& b) const {
if (a.rating == b.rating) return a.food > b.food;
return a.rating < b.rating;
}
};
unordered_map<string, priority_queue<Node, vector<Node>, Cmp>> cuisines_heap;

public:
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
int n = (int)foods.size();
for (int i = 0; i < n; ++i) {
const string& food = foods[i];
const string& cuisine = cuisines[i];
int rating = ratings[i];
food_cuisines[food] = cuisine;
food_ratings[food] = rating;
cuisines_heap[cuisine].push({rating, food});
}
}

void changeRating(string food, int newRating) {
const string& cuisine = food_cuisines[food];
food_ratings[food] = newRating;
cuisines_heap[cuisine].push({newRating, food});
}

string highestRated(string cuisine) {
auto& pq = cuisines_heap[cuisine];
while (!pq.empty()) {
const auto top = pq.top();
if (food_ratings[top.food] == top.rating) return top.food;
pq.pop();
}
return "";
}
};

/**
* Your FoodRatings object will be instantiated and called as such:
* FoodRatings* obj = new FoodRatings(foods, cuisines, ratings);
* obj->changeRating(food,newRating);
* string param_2 = obj->highestRated(cuisine);
*/
Loading