Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Design a food rating system that can do the following:
Implement the FoodRatings
class:
FoodRatings(String[] foods, String[] cuisines, int[] ratings)
Initializes the system. The food items are described by foods
, cuisines
and ratings
, all of which have a length of n
.foods[i]
is the name of the ith
food,cuisines[i]
is the type of cuisine of the ith
food, andratings[i]
is the initial rating of the ith
food.void changeRating(String food, int newRating)
Changes the rating of the food item with the name food
.String highestRated(String cuisine)
Returns the name of the food item that has the highest rating for the given type of cuisine
. If there is a tie, return the item with the lexicographically smaller name.Note that a string x
is lexicographically smaller than string y
if x
comes before y
in dictionary order, that is, either x
is a prefix of y
, or if i
is the first position such that x[i] != y[i]
, then x[i]
comes before y[i]
in alphabetic order.
Example 1:
Input
["FoodRatings", "highestRated", "highestRated", "changeRating", "highestRated", "changeRating", "highestRated"]
[[["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]], ["korean"], ["japanese"], ["sushi", 16], ["japanese"], ["ramen", 16], ["japanese"]]
Output
[null, "kimchi", "ramen", null, "sushi", null, "ramen"]
Explanation
FoodRatings foodRatings = new FoodRatings(["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]);
foodRatings.highestRated("korean"); // return "kimchi"
// "kimchi" is the highest rated korean food with a rating of 9.
foodRatings.highestRated("japanese"); // return "ramen"
// "ramen" is the highest rated japanese food with a rating of 14.
foodRatings.changeRating("sushi", 16); // "sushi" now has a rating of 16.
foodRatings.highestRated("japanese"); // return "sushi"
// "sushi" is the highest rated japanese food with a rating of 16.
foodRatings.changeRating("ramen", 16); // "ramen" now has a rating of 16.
foodRatings.highestRated("japanese"); // return "ramen"
// Both "sushi" and "ramen" have a rating of 16.
// However, "ramen" is lexicographically smaller than "sushi".
Constraints:
1 <= n <= 2 * 104
n == foods.length == cuisines.length == ratings.length
1 <= foods[i].length, cuisines[i].length <= 10
foods[i]
, cuisines[i]
consist of lowercase English letters.1 <= ratings[i] <= 108
foods
are distinct.food
will be the name of a food item in the system across all calls to changeRating
.cuisine
will be a type of cuisine of at least one food item in the system across all calls to highestRated
.2 * 104
calls in total will be made to changeRating
and highestRated
.class FoodRatings {
public:
unordered_map<string, set<pair<int, string>>> cuisine_ratings;
unordered_map<string, string> food_cuisine;
unordered_map<string, int> food_rating;
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
for (int i = 0; i < foods.size(); ++i) {
cuisine_ratings[cuisines[i]].insert({ -ratings[i], foods[i] });
food_cuisine[foods[i]] = cuisines[i];
food_rating[foods[i]] = ratings[i];
}
}
void changeRating(string food, int newRating) {
auto &cuisine = food_cuisine.find(food)->second;
cuisine_ratings[cuisine].erase({ -food_rating[food], food });
cuisine_ratings[cuisine].insert({ -newRating, food });
food_rating[food] = newRating;
}
string highestRated(string cuisine) {
return begin(cuisine_ratings[cuisine])->second;
}
};
HashMap<String, PriorityQueue<Food>> x = new HashMap<>(); // get pq from cuisine name
HashMap<String, Food> menu = new HashMap<>(); // get Food (object) by food name
public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
for(int i=0; i<foods.length; i++){
if(!x.containsKey(cuisines[i])){
PriorityQueue<Food> pq = new PriorityQueue<>((a,b)->
b.rating-a.rating==0 ? a.name.compareTo(b.name) : b.rating-a.rating);
x.put(cuisines[i], pq);
}
Food curr = new Food(foods[i], cuisines[i], ratings[i]);
PriorityQueue<Food> pq = x.get(cuisines[i]);
pq.add(curr);
menu.put(foods[i], curr);
}
}
public void changeRating(String food, int newRating) {
Food curr = menu.get(food);
PriorityQueue<Food> pq = x.get(curr.cuisine);
pq.remove(curr);
curr.rating = newRating;
pq.add(curr);
}
public String highestRated(String cuisine) {
return x.get(cuisine).peek().name;
}
class Food{
int rating;
String name, cuisine;
Food(String name, String cuisine, int rating){
this.name = name; this.rating = rating; this.cuisine = cuisine;
}
}
class FoodRatings:
def __init__(self, foods: List[str], cuisines: List[str], ratings: List[int]):
n = len(foods)
self.foods = foods; self.cuisines = cuisines; self.ratings = ratings
self.index = dict(zip(foods, range(n)))
self.cuisinesMap = defaultdict(list)
for i, cuisine in enumerate(cuisines):
heappush(self.cuisinesMap[cuisine], (-self.ratings[i], self.foods[i]))
def changeRating(self, food: str, newRating: int) -> None:
i = self.index[food]
self.ratings[i] = newRating; cuisine = self.cuisines[i]
heappush(self.cuisinesMap[cuisine], (-self.ratings[i], self.foods[i]))
def highestRated(self, cuisine: str) -> str:
rating, food = self.cuisinesMap[cuisine][0]
while rating != -self.ratings[self.index[food]]: # check highest rating is current
heappop(self.cuisinesMap[cuisine])
rating, food = self.cuisinesMap[cuisine][0]
return food
In our experience, we suggest you solve this Design a Food Rating System LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Design a Food Rating System LeetCode Solution
I hope this Design a Food Rating System LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>