diff --git a/2353. Design a Food Rating System 1 b/2353. Design a Food Rating System 1 new file mode 100644 index 0000000..aa11a89 --- /dev/null +++ b/2353. Design a Food Rating System 1 @@ -0,0 +1,51 @@ +class FoodRatings { + unordered_map food_cuisines; + unordered_map 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, Cmp>> cuisines_heap; + +public: + FoodRatings(vector& foods, vector& cuisines, vector& 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); + */