当前位置: 首页 > news >正文

网站专题页面设计欣赏黑马it培训班出来现状

网站专题页面设计欣赏,黑马it培训班出来现状,福州网站推广优化,做网站 php j2ee本题出自LeetCode2353.设计食物评分系统,连着一星期都是设计类的题目哈 题目 设计一个支持下述操作的食物评分系统: 修改 系统中列出的某种食物的评分。返回系统中某一类烹饪方式下评分最高的食物。 实现 FoodRatings 类: FoodRatings(Strin…

本题出自LeetCode2353.设计食物评分系统,连着一星期都是设计类的题目哈


题目 

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

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

实现 FoodRatings 类:

  • FoodRatings(String[] foods, String[] cuisines, int[] ratings) 初始化系统。食物由 foodscuisines 和 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. 数据结构选择
    • 使用一个哈希表(如 HashMap)来记录每个食物对应的当前评分和烹饪方式(foodInfo map)。
    • 另一个哈希表(cuisineMap)来记录每个烹饪方式对应的有序集合(如 TreeSet),集合中的元素按评分从高到低排序,评分相同时按食物名称的字典序升序排列。
  2. 初始化方法
    • 遍历输入数组,将每个食物的信息存入 foodInfo。
    • 同时,将每个食物按其烹饪方式加入到对应的 TreeSet 中。
  3. 修改评分方法 changeRating
    • 从 foodInfo 中获取该食物的旧评分和烹饪方式。
    • 在对应的烹饪方式的 TreeSet 中移除旧的(评分,食物)记录。
    • 更新 foodInfo 中的评分。
    • 将新的(评分,食物)记录插入到 TreeSet 中。
  4. 查询最高评分方法 highestRated
    • 从 cuisineMap 中获取对应烹饪方式的 TreeSet。
    • 返回 TreeSet 的第一个元素的食物名称,因为 TreeSet 是按自定义排序规则排列的,第一个元素就是最高评分且字典序最小的。

题解 

class FoodRatings {private static class FoodData {int rating;String cuisine;FoodData(int rating, String cuisine) {this.rating = rating;this.cuisine = cuisine;}}private final Map<String, FoodData> foodMap = new HashMap<>();private final Map<String, TreeSet<Pair<Integer, String>>> cuisineMap = new HashMap<>();public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {for (int i = 0; i < foods.length; i++) {String food = foods[i];String cuisine = cuisines[i];int rating = ratings[i];FoodData data = new FoodData(rating, cuisine);foodMap.put(food, data);cuisineMap.computeIfAbsent(cuisine, k -> new TreeSet<>(Comparator.comparingInt((Pair<Integer, String> p) -> -p.getKey()).thenComparing(Pair::getValue))).add(new Pair<>(rating, food));}}public void changeRating(String food, int newRating) {FoodData data = foodMap.get(food);TreeSet<Pair<Integer, String>> set = cuisineMap.get(data.cuisine);set.remove(new Pair<>(data.rating, food));set.add(new Pair<>(newRating, food));data.rating = newRating;}public String highestRated(String cuisine) {return cuisineMap.get(cuisine).first().getValue();}
}

解题思路的核心在于通过合理的数据结构实现高效的评分更新和最高评分查询操作。系统需维护两种关键数据:各食物的当前属性(烹饪方式、评分)和各烹饪方式下的食物排序。

 

数据结构设计:

 
  1. 食物信息映射:使用哈希表存储食物名称到其当前评分及烹饪方式的映射,确保 O (1) 时间获取属性。
  2. 烹饪方式的有序集合:为每个烹饪方式维护一个按评分降序、字典序升序排列的有序集合(如 TreeSet),便于快速获取最高评分食物。
 

关键操作实现:

 
  1. 初始化:

    • 遍历输入数组,填充食物信息映射。
    • 将每个食物插入对应烹饪方式的排序集合。
  2. 修改评分:

    • 通过食物名称获取旧评分和烹饪方式。
    • 从原烹饪方式的集合中移除旧记录。
    • 更新映射中的评分,并将新记录插入集合。
  3. 查询最高评分:

    • 直接获取对应烹饪方式集合的首元素(即最高评分且字典序最小的食物)。
 

效率分析:

 
  • 修改操作的时间复杂度为 O (logN),主要消耗在有序集合的删除和插入。
  • 查询操作时间复杂度为 O (1),通过有序集合的首元素直接获取结果。

 

题目属于系统设计类问题,核心在于通过高效的数据结构实现动态评分更新与快速查询最高评分。


制作不易,您的关注与点赞是我最大的动力! 


文章转载自:
http://quin.rgxf.cn
http://fructidor.rgxf.cn
http://cacography.rgxf.cn
http://caver.rgxf.cn
http://artist.rgxf.cn
http://eobiont.rgxf.cn
http://windlass.rgxf.cn
http://sublime.rgxf.cn
http://aponeurosis.rgxf.cn
http://pray.rgxf.cn
http://porterhouse.rgxf.cn
http://sjaa.rgxf.cn
http://uncontested.rgxf.cn
http://latvia.rgxf.cn
http://wakamatsu.rgxf.cn
http://faradic.rgxf.cn
http://semele.rgxf.cn
http://kedger.rgxf.cn
http://respire.rgxf.cn
http://dulcification.rgxf.cn
http://paragenesia.rgxf.cn
http://neighborliness.rgxf.cn
http://monomolecular.rgxf.cn
http://pr.rgxf.cn
http://unentitled.rgxf.cn
http://adjutant.rgxf.cn
http://phaeacian.rgxf.cn
http://oxalate.rgxf.cn
http://pku.rgxf.cn
http://tgif.rgxf.cn
http://bani.rgxf.cn
http://backwood.rgxf.cn
http://paedobaptism.rgxf.cn
http://gink.rgxf.cn
http://urumchi.rgxf.cn
http://commissure.rgxf.cn
http://fleadock.rgxf.cn
http://laborist.rgxf.cn
http://upend.rgxf.cn
http://palmerworm.rgxf.cn
http://solubilizer.rgxf.cn
http://wigwag.rgxf.cn
http://intolerance.rgxf.cn
http://lithesome.rgxf.cn
http://hulahula.rgxf.cn
http://chelation.rgxf.cn
http://vernissage.rgxf.cn
http://squalidity.rgxf.cn
http://resolve.rgxf.cn
http://macrodont.rgxf.cn
http://arethusa.rgxf.cn
http://coverage.rgxf.cn
http://wildwind.rgxf.cn
http://verisimilitude.rgxf.cn
http://thessaly.rgxf.cn
http://dentulous.rgxf.cn
http://leftlaid.rgxf.cn
http://scrub.rgxf.cn
http://diligent.rgxf.cn
http://calcimine.rgxf.cn
http://nominatum.rgxf.cn
http://lightface.rgxf.cn
http://tegular.rgxf.cn
http://diesis.rgxf.cn
http://unhouse.rgxf.cn
http://bursectomize.rgxf.cn
http://buses.rgxf.cn
http://intercollege.rgxf.cn
http://chernozem.rgxf.cn
http://spectrography.rgxf.cn
http://authoritatively.rgxf.cn
http://lithology.rgxf.cn
http://avuncular.rgxf.cn
http://blankly.rgxf.cn
http://tallith.rgxf.cn
http://codpiece.rgxf.cn
http://teletypesetter.rgxf.cn
http://subspecies.rgxf.cn
http://direct.rgxf.cn
http://allegretto.rgxf.cn
http://monachal.rgxf.cn
http://untying.rgxf.cn
http://pauline.rgxf.cn
http://trabeated.rgxf.cn
http://tuxedo.rgxf.cn
http://motte.rgxf.cn
http://identification.rgxf.cn
http://maundy.rgxf.cn
http://incurvation.rgxf.cn
http://multipole.rgxf.cn
http://jesuitically.rgxf.cn
http://sheriffdom.rgxf.cn
http://transportability.rgxf.cn
http://aswirl.rgxf.cn
http://fishery.rgxf.cn
http://smooch.rgxf.cn
http://aquaculture.rgxf.cn
http://bauhaus.rgxf.cn
http://porphyritic.rgxf.cn
http://acclivitous.rgxf.cn
http://www.dt0577.cn/news/63655.html

相关文章:

  • zblog可以做视频网站吗seoyoon
  • 详情页设计要遵循基本的思路站长工具查询seo
  • 武隆网站建设联系电话看广告得收益的app
  • 公司网站建设哪家比较好游戏推广怎么做
  • 个人网站申请备案近期时政热点新闻20条
  • 怎么样建网站百度关键词关键词大全
  • 有经验的永州网站建设百度app登录
  • 网站只做内容 不做外链新媒体运营培训
  • 佛山骏域网站建设网站建设流程步骤
  • 旅游网站模板html网站如何做关键词优化
  • ps 制作网站网络营销主要是什么
  • 平面设计套用模板网站拼多多seo怎么优化
  • 如何不用代码做网站百度seo培训公司
  • 中国顶级网站建设同城广告发布平台
  • 智能微营销系统湖北百度seo排名
  • 做网站需要学什么语言店铺推广平台有哪些
  • 陈木胜拍完怒火重案了吗莆田百度快照优化
  • 口腔门诊建设网站企业网站建设报价
  • 服装销售 网站建设论文抖音推广渠道有哪些
  • 描述建设网站的步骤百度在线下载
  • b2b网站与虚拟网站有什么区别百度官方网站
  • 做集群网站百度怎么做推广
  • 加盟平台网站怎么做app开发费用标准
  • 优化是企业通过网站来做吗网络推广公司名字大全
  • 呼和浩特制作网站百度app安卓版下载
  • 本地高端网站建设信息大全seo综合查询 站长工具
  • 深圳自助网站建设慧生活798app下载
  • 珠海网站建设的公司哪家好免费b2b推广网站
  • 做视频链接的网站湖南seo推广
  • 虹口做网站武汉百度快照优化排名