2353. Design a Food Rating System Medium
1/**
2 * [2353] Design a Food Rating System
3 *
4 * Design a food rating system that can do the following:
5 *
6 * Modify the rating of a food item listed in the system.
7 * Return the highest-rated food item for a type of cuisine in the system.
8 *
9 * Implement the FoodRatings class:
10 *
11 * 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.
12 *
13 * foods[i] is the name of the i^th food,
14 * cuisines[i] is the type of cuisine of the i^th food, and
15 * ratings[i] is the initial rating of the i^th food.
16 *
17 *
18 * void changeRating(String food, int newRating) Changes the rating of the food item with the name food.
19 * 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.
20 *
21 * 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.
22 *
23 * Example 1:
24 *
25 * Input
26 * ["FoodRatings", "highestRated", "highestRated", "changeRating", "highestRated", "changeRating", "highestRated"]
27 * [[["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"]]
28 * Output
29 * [null, "kimchi", "ramen", null, "sushi", null, "ramen"]
30 * Explanation
31 * FoodRatings foodRatings = new FoodRatings(["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]);
32 * foodRatings.highestRated("korean"); // return "kimchi"
33 * // "kimchi" is the highest rated korean food with a rating of 9.
34 * foodRatings.highestRated("japanese"); // return "ramen"
35 * // "ramen" is the highest rated japanese food with a rating of 14.
36 * foodRatings.changeRating("sushi", 16); // "sushi" now has a rating of 16.
37 * foodRatings.highestRated("japanese"); // return "sushi"
38 * // "sushi" is the highest rated japanese food with a rating of 16.
39 * foodRatings.changeRating("ramen", 16); // "ramen" now has a rating of 16.
40 * foodRatings.highestRated("japanese"); // return "ramen"
41 * // Both "sushi" and "ramen" have a rating of 16.
42 * // However, "ramen" is lexicographically smaller than "sushi".
43 *
44 *
45 * Constraints:
46 *
47 * 1 <= n <= 2 * 10^4
48 * n == foods.length == cuisines.length == ratings.length
49 * 1 <= foods[i].length, cuisines[i].length <= 10
50 * foods[i], cuisines[i] consist of lowercase English letters.
51 * 1 <= ratings[i] <= 10^8
52 * All the strings in foods are distinct.
53 * food will be the name of a food item in the system across all calls to changeRating.
54 * cuisine will be a type of cuisine of at least one food item in the system across all calls to highestRated.
55 * At most 2 * 10^4 calls in total will be made to changeRating and highestRated.
56 *
57 */
58pub struct Solution {}
59
60// problem: https://leetcode.com/problems/design-a-food-rating-system/
61// discuss: https://leetcode.com/problems/design-a-food-rating-system/discuss/?currentPage=1&orderBy=most_votes&query=
62
63// submission codes start here
64
65struct FoodRatings {
66 false
67 }
68
69
70/**
71 * `&self` means the method takes an immutable reference.
72 * If you need a mutable reference, change it to `&mut self` instead.
73 */
74impl FoodRatings {
75
76 fn new(foods: Vec<String>, cuisines: Vec<String>, ratings: Vec<i32>) -> Self {
77
78 }
79
80 fn change_rating(&self, food: String, new_rating: i32) {
81
82 }
83
84 fn highest_rated(&self, cuisine: String) -> String {
85
86 }
87}
88
89/**
90 * Your FoodRatings object will be instantiated and called as such:
91 * let obj = FoodRatings::new(foods, cuisines, ratings);
92 * obj.change_rating(food, newRating);
93 * let ret_2: String = obj.highest_rated(cuisine);
94 */
95
96// submission codes end
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101
102 #[test]
103 fn test_2353() {
104 }
105}
106
Back
© 2025 bowen.ge All Rights Reserved.