Design a Food Rating System - Problem
Design a food rating system that supports the following operations:
- Modify the rating of a food item in the system
- Return the highest-rated food item for a specific cuisine type
Implement the FoodRatings class:
FoodRatings(String[] foods, String[] cuisines, int[] ratings)- Initializes the system with food items, their cuisines, and initial ratingsvoid changeRating(String food, int newRating)- Changes the rating of the specified food itemString highestRated(String cuisine)- Returns the name of the highest-rated food for the given cuisine. If there's a tie, return the lexicographically smaller name
Input & Output
Example 1 — Basic Operations
$
Input:
operations = [["FoodRatings", ["kimchi", "miso", "sushi"], ["korean", "japanese", "japanese"], [9, 12, 8]], ["highestRated", "japanese"], ["changeRating", "sushi", 16], ["highestRated", "japanese"]]
›
Output:
["miso", "sushi"]
💡 Note:
Initially miso (12) > sushi (8) for japanese cuisine. After updating sushi to 16, sushi (16) > miso (12).
Example 2 — Lexicographic Tiebreaker
$
Input:
operations = [["FoodRatings", ["apple", "banana"], ["fruit", "fruit"], [5, 5]], ["highestRated", "fruit"]]
›
Output:
["apple"]
💡 Note:
Both have rating 5, so return lexicographically smaller: apple < banana.
Example 3 — Single Item Cuisine
$
Input:
operations = [["FoodRatings", ["pizza"], ["italian"], [10]], ["highestRated", "italian"]]
›
Output:
["pizza"]
💡 Note:
Only one item in italian cuisine, so return pizza.
Constraints
- 1 ≤ n ≤ 2×104
- 1 ≤ foods[i].length, cuisines[i].length ≤ 10
- 1 ≤ ratings[i] ≤ 108
- At most 2×104 calls to changeRating and highestRated
Visualization
Tap to expand
Understanding the Visualization
1
Input
Foods with cuisines and ratings
2
Operations
Support rating updates and highest-rated queries
3
Output
Return best food per cuisine (lexicographic tiebreaker)
Key Takeaway
🎯 Key Insight: Hash maps provide O(1) food access while heaps maintain sorted order per cuisine for fast queries
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code