• LeetCode 2353. 设计食物评分系统(sortedcontainers)


    文章目录

    1. 题目

    设计一个支持下述操作的食物评分系统:

    • 修改 系统中列出的某种食物的评分。
    • 返回系统中某一类烹饪方式下评分最高的食物。

    实现 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" 更小。
     
    提示:
    1 <= n <= 2 * 10^4
    n == foods.length == cuisines.length == ratings.length
    1 <= foods[i].length, cuisines[i].length <= 10
    foods[i]、cuisines[i] 由小写英文字母组成
    1 <= ratings[i] <= 10^8
    foods 中的所有字符串 互不相同
    在对 changeRating 的所有调用中,food 是系统中食物的名字。
    在对 highestRated 的所有调用中,cuisine 是系统中 至少一种 食物的烹饪方式。
    最多调用 changeRating 和 highestRated 总计 2 * 10^4
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    来源:力扣(LeetCode
    链接:https://leetcode.cn/problems/design-a-food-rating-system
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    2. 解题

    • 使用 python 的有序 set from sortedcontainers import SortedSet,或者 C++ 的 set
    from collections import defaultdict
    from sortedcontainers import SortedSet
    class FoodRatings:
    
        def __init__(self, foods: List[str], cuisines: List[str], ratings: List[int]):
            self.s = defaultdict(SortedSet)
            self.map = {}
            for food, cuisine, rate in zip(foods, cuisines, ratings):
                self.s[cuisine].add((-rate, food))
                self.map[food] = [-rate, cuisine]
    
        def changeRating(self, food: str, newRating: int) -> None:
            rate, cuisine = self.map[food]
            self.s[cuisine].discard((rate, food))
            self.map[food] = [-newRating, cuisine]
            self.s[cuisine].add((-newRating, food))
    
        def highestRated(self, cuisine: str) -> str:
            return self.s[cuisine][0][1]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    708 ms 48.7 MB Python3


    我的CSDN博客地址 https://michael.blog.csdn.net/

    长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
    Michael阿明

  • 相关阅读:
    文件上传漏洞--Upload-labs--Pass17--条件竞争
    浏览器网络无法连接github的解决办法
    Magento_CentOS安装
    git常见问题
    [ABC098D] Xor Sum 2 【双指针】
    李永乐线代笔记
    希尔伯特变换分析及应用
    rust 快速一览
    websocket协议原理
    mulesoft Module 7 quiz 解析
  • 原文地址:https://blog.csdn.net/qq_21201267/article/details/126069358