#2353

Design a Food Rating System

specialist · 840 · lc medium +31 · 52.9% accepted · 1,958 likes · top 43%

Description

Build a system for rating food items by cuisine, supporting two operations:

- Update the rating of a named food item.

- Query the top-rated food item within a given cuisine type.

Implement the FoodRatings class:

- FoodRatings(String[] foods, String[] cuisines, int[] ratings) sets up the system with n food items. foods[i] is the item name, cuisines[i] is its cuisine type, and ratings[i] is its initial rating.

- void changeRating(String food, int newRating) updates the rating of the food item named food.

- String highestRated(String cuisine) returns the name of the food with the highest rating in cuisine. Break ties by returning the lexicographically smaller name.

A string x is lexicographically smaller than y if it precedes y in dictionary 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"]

Example 2:

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".

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16