|
| 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 | + */ |
| 58 | +pub 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 | + |
| 65 | +struct FoodRatings {} |
| 66 | + |
| 67 | +/** |
| 68 | + * `&self` means the method takes an immutable reference. |
| 69 | + * If you need a mutable reference, change it to `&mut self` instead. |
| 70 | + */ |
| 71 | +impl FoodRatings { |
| 72 | + fn new(foods: Vec<String>, cuisines: Vec<String>, ratings: Vec<i32>) -> Self { |
| 73 | + Self {} |
| 74 | + } |
| 75 | + |
| 76 | + fn change_rating(&self, food: String, new_rating: i32) { |
| 77 | + // |
| 78 | + } |
| 79 | + |
| 80 | + fn highest_rated(&self, cuisine: String) -> String { |
| 81 | + String::new() |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Your FoodRatings object will be instantiated and called as such: |
| 87 | + * let obj = FoodRatings::new(foods, cuisines, ratings); |
| 88 | + * obj.change_rating(food, newRating); |
| 89 | + * let ret_2: String = obj.highest_rated(cuisine); |
| 90 | + */ |
| 91 | + |
| 92 | +// submission codes end |
| 93 | + |
| 94 | +#[cfg(test)] |
| 95 | +mod tests { |
| 96 | + use super::*; |
| 97 | + |
| 98 | + #[test] |
| 99 | + #[ignore] |
| 100 | + fn test_2353_example_1() { |
| 101 | + let mut food_ratings = FoodRatings::new( |
| 102 | + vec_string!["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], |
| 103 | + vec_string![ |
| 104 | + "korean", "japanese", "japanese", "greek", "japanese", "korean", |
| 105 | + ], |
| 106 | + vec![9, 12, 8, 15, 14, 7], |
| 107 | + ); |
| 108 | + assert_eq!( |
| 109 | + food_ratings.highest_rated("korean".to_string()), |
| 110 | + "kimchi".to_string() |
| 111 | + ); |
| 112 | + |
| 113 | + assert_eq!( |
| 114 | + food_ratings.highest_rated("japanese".to_string()), |
| 115 | + "ramen".to_string() |
| 116 | + ); |
| 117 | + |
| 118 | + food_ratings.change_rating("sushi".to_string(), 16); |
| 119 | + assert_eq!( |
| 120 | + food_ratings.highest_rated("japanese".to_string()), |
| 121 | + "sushi".to_string() |
| 122 | + ); |
| 123 | + |
| 124 | + food_ratings.change_rating("ramen".to_string(), 16); |
| 125 | + assert_eq!( |
| 126 | + food_ratings.highest_rated("japanese".to_string()), |
| 127 | + "ramen".to_string() |
| 128 | + ); |
| 129 | + } |
| 130 | +} |
0 commit comments