Design a Food Rating System

Medium Topics Company Tags

Implement FoodRatings to track foods, their cuisines, and their ratings.

  • FoodRatings(foods, cuisines, ratings) initializes one food from each matching index in the three arrays.
  • changeRating(food, newRating) replaces the rating of an existing food.
  • highestRated(cuisine) returns the highest-rated food of that cuisine. When several foods share that rating, return the lexicographically smallest name.

Lexicographic order is dictionary order. If one name is a prefix of another, the shorter name comes first. Updates affect only the specified food; its cuisine stays the same.

Example 1:

Input: ["FoodRatings","highestRated","changeRating","highestRated"]
[[["apple","apricot"],["fruit","fruit"],[5,5]],["fruit"],["apricot",8],["fruit"]]

Output: [null,"apple",null,"apricot"]

Explanation: Apple and apricot start tied at 5, so apple wins by alphabetical order. Increasing apricot to 8 makes it the highest-rated fruit.

Example 2:

Input: ["FoodRatings","highestRated","changeRating","highestRated"]
[[["rice","soup"],["asian","asian"],[9,7]],["asian"],["rice",3],["asian"]]

Output: [null,"rice",null,"soup"]

Explanation: Rice initially has the higher rating. After its rating drops to 3, soup becomes the highest-rated Asian food with a rating of 7.

Example 3:

Input: ["FoodRatings","highestRated","highestRated"]
[[["bread","beans","berry"],["grain","plant","plant"],[2,6,6]],["grain"],["plant"]]

Output: [null,"bread","beans"]

Explanation: Bread is the only grain food. Beans and berry tie within the plant cuisine, so beans is returned because its name comes first alphabetically.

Constraints:

  • 1 <= foods.length == cuisines.length == ratings.length <= 20000
  • Food and cuisine names contain 1 to 10 lowercase English letters.
  • All food names are distinct.
  • 1 <= ratings[i], newRating <= 100000000
  • Every update refers to an existing food, and every query refers to an existing cuisine.
  • At most 20000 calls are made to changeRating and highestRated combined.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.