Leetcode 2353: Design a Food Rating System

grid47
grid47
Exploring patterns and algorithms
Mar 16, 2024 6 min read

Design a food rating system that allows modification of food ratings and can return the highest-rated food item for a given cuisine. In case of a tie, the lexicographically smallest food item should be returned.
Problem
Approach
Steps
Complexity
Input: The input consists of the names of food items, the cuisines they belong to, and their corresponding ratings.
Example: Input: foods = ["pasta", "sushi", "tacos", "paella", "sushi", "pizza"], cuisines = ["italian", "japanese", "mexican", "spanish", "japanese", "italian"], ratings = [9, 12, 8, 15, 14, 7]
Constraints:
• 1 <= n <= 2 * 10^4
• 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] <= 10^8
• All food names in the system are distinct.
Output: Output the name of the food item with the highest rating for a given cuisine, or the lexicographically smaller one in case of a tie.
Example: Output: "pasta" for highestRated("italian") when pasta has the highest rating.
Constraints:
• The system handles all queries efficiently with a time complexity suited to the input constraints.
Goal: Efficiently maintain and update the ratings for each food item and return the highest-rated food for each cuisine.
Steps:
• Store the food items, their ratings, and corresponding cuisines in appropriate data structures.
• For each cuisine, maintain a set of food items ordered by rating (and lexicographically for ties).
• Update the ratings dynamically upon calls to changeRating and ensure the highestRated function returns the correct food item.
Goal: The problem must handle up to 20,000 food items and efficiently process queries.
Steps:
• At most 2 * 10^4 operations (calls to changeRating and highestRated).
Assumptions:
• The system starts with a fixed number of food items and their ratings.
• All food names are distinct and each call to changeRating uses a valid food name.
Input: Input: foods = ["pasta", "sushi", "tacos", "paella", "sushi", "pizza"], cuisines = ["italian", "japanese", "mexican", "spanish", "japanese", "italian"], ratings = [9, 12, 8, 15, 14, 7]
Explanation: After the initialization, "pasta" is the highest rated italian food, "sushi" is the highest rated japanese food. After changing the rating of "sushi" to 16, "sushi" becomes the highest rated japanese food.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus