携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第17天,点击查看活动详情
题目描述
设计一个支持下述操作的食物评分系统:
修改 系统中列出的某种食物的评分。 返回系统中某一类烹饪方式下评分最高的食物。 实现 FoodRatings 类:
FoodRatings(String[] foods, String[] cuisines, int[] ratings) 初始化系统。食物由 foods、cuisines 和 ratings 描述,长度均为 n 。 foods[i] 是第 i 种食物的名字。 cuisines[i] 是第 i 种食物的烹饪方式。 ratings[i] 是第 i 种食物的最初评分。 void changeRating(String food, int newRating) 修改名字为 food 的食物的评分。 String highestRated(String cuisine) 返回指定烹饪方式 cuisine 下评分最高的食物的名字。如果存在并列,返回 字典序较小 的名字。 注意,字符串 x 的字典序比字符串 y 更小的前提是:x 在字典中出现的位置在 y 之前,也就是说,要么 x 是 y 的前缀,或者在满足 x[i] != y[i] 的第一个位置 i 处,x[i] 在字母表中出现的位置在 y[i] 之前。
示例:
输入
["FoodRatings", "highestRated", "highestRated", "changeRating", "highestRated", "changeRating", "highestRated"]
[[["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"]]
输出
[null, "kimchi", "ramen", null, "sushi", null, "ramen"]
解释
FoodRatings foodRatings = new FoodRatings(["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]);
foodRatings.highestRated("korean"); // 返回 "kimchi"
// "kimchi" 是分数最高的韩式料理,评分为 9 。
foodRatings.highestRated("japanese"); // 返回 "ramen"
// "ramen" 是分数最高的日式料理,评分为 14 。
foodRatings.changeRating("sushi", 16); // "sushi" 现在评分变更为 16 。
foodRatings.highestRated("japanese"); // 返回 "sushi"
// "sushi" 是分数最高的日式料理,评分为 16 。
foodRatings.changeRating("ramen", 16); // "ramen" 现在评分变更为 16 。
foodRatings.highestRated("japanese"); // 返回 "ramen"
// "sushi" 和 "ramen" 的评分都是 16 。
// 但是,"ramen" 的字典序比 "sushi" 更小。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/design-a-food-rating-system
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路分析
- 今天的的算法题目是设计类题目。题目描述比较长,我们跟着例子来理解。
- 题目给出的是食物,评分,烹饪方式。highestRated() 需要返回指定烹饪方式 cuisine 下评分最高的食物的名字。如果存在并列,返回 字典序较小 的名字。根据这个要求分析,这里需要实现一个多纬度排序的功能。在Java中 TreeSet类提供了使用树进行存储的Set接口的实现。对象按排序和升序存储。访问和检索时间非常快,这使得TreeSet成为存储必须快速找到的大量排序信息的绝佳选择。
- 我们自定义 FoodRate 类,存储food,rate, cuisines 三个属性。方便在 TreeSet 中按照先 rate, 后 food 属性排序。定义 foodRateMap 存储对象关系。当调用 changeRating 方法的时候,评分变动,我们需要对 TreeSet 数据重新排序。先处理旧对象值的相关操作,然后在处理新对象的相关操作。保证数据的一致性。
- 具体实现代码如下,供参考。
通过代码
class FoodRatings {
Map<String, TreeSet<FoodRate>> cuisinesMap;
Map<String, FoodRate> foodRateMap;
public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
cuisinesMap = new HashMap<>();
foodRateMap = new HashMap<>();
int n = foods.length;
String foodName;
String cuisinesName;
Integer rate;
for (int i = 0; i < n; i++) {
foodName = foods[i];
cuisinesName = cuisines[i];
rate = ratings[i];
TreeSet<FoodRate> set = cuisinesMap.getOrDefault(cuisinesName, new TreeSet<>(new Comparator<FoodRate>() {
@Override
public int compare(FoodRate o1, FoodRate o2) {
if (o1.rate != o2.rate) {
return o2.rate - o1.rate;
}
return o1.food.compareTo(o2.food);
}
}));
FoodRate foodRate = new FoodRate(foodName, rate, cuisinesName);
foodRateMap.put(foodName, foodRate);
set.add(foodRate);
cuisinesMap.put(cuisinesName, set);
}
}
public void changeRating(String food, int newRating) {
FoodRate oldFoodRate = foodRateMap.get(food);
TreeSet<FoodRate> set = cuisinesMap.get(oldFoodRate.cuisines);
set.remove(oldFoodRate);
FoodRate newFoodRate = new FoodRate(food, newRating, oldFoodRate.cuisines);
set.add(newFoodRate);
cuisinesMap.put(oldFoodRate.cuisines, set);
foodRateMap.put(food, newFoodRate);
}
public String highestRated(String cuisine) {
return cuisinesMap.get(cuisine).first().food;
}
class FoodRate {
String food;
int rate;
String cuisines;
public FoodRate(String food, int rate, String cuisines) {
this.food = food;
this.rate = rate;
this.cuisines = cuisines;
}
}
}
总结
- 在Java中排序,对于一般的纯数字,字符串排序,我们直接调用 sort() 系统函数即可, sort() 函数默认是自然排序。
- 当需要按照自然倒叙排列时候,在sort() 中我们可以自定义 Comparator, Override compare() 方法实现排序。
- 当需要按照多个属性排序的时候,我们一般可以定义一个对象,包含需要排序的属性,然后自定义 Comparator,实现多属性纬度排序。比如:使用 TreeSet 这种数据结构,需要注意的是, 当对象进行修改的时候,我们必须先删除旧的对象,然后在重新插入新的对象。
- 坚持算法每日一题,加油!