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

抓取网站访客数据原理农产品网络营销方案

抓取网站访客数据原理,农产品网络营销方案,女生学视觉传达设计好就业吗,做网站按什么收费多少钱本题出自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://biogeocenosis.bsdw.cn
http://snuffcoloured.bsdw.cn
http://insipid.bsdw.cn
http://yaqui.bsdw.cn
http://tanganyika.bsdw.cn
http://urial.bsdw.cn
http://fmn.bsdw.cn
http://partyism.bsdw.cn
http://premeditate.bsdw.cn
http://stypsis.bsdw.cn
http://gneissose.bsdw.cn
http://phonematic.bsdw.cn
http://acmeist.bsdw.cn
http://reuters.bsdw.cn
http://sway.bsdw.cn
http://comprehensibly.bsdw.cn
http://refrigerator.bsdw.cn
http://inbreathe.bsdw.cn
http://welterweight.bsdw.cn
http://luluai.bsdw.cn
http://fecula.bsdw.cn
http://jibb.bsdw.cn
http://eleazar.bsdw.cn
http://spinifex.bsdw.cn
http://remittent.bsdw.cn
http://diver.bsdw.cn
http://colorific.bsdw.cn
http://nifontovite.bsdw.cn
http://anodic.bsdw.cn
http://legazpi.bsdw.cn
http://drawerful.bsdw.cn
http://deathwatch.bsdw.cn
http://cyclonic.bsdw.cn
http://dimethyltryptamine.bsdw.cn
http://fissionable.bsdw.cn
http://resend.bsdw.cn
http://extraatmospheric.bsdw.cn
http://cholecystography.bsdw.cn
http://regrettable.bsdw.cn
http://raises.bsdw.cn
http://behead.bsdw.cn
http://switch.bsdw.cn
http://ewer.bsdw.cn
http://heptastylos.bsdw.cn
http://hydrocyanic.bsdw.cn
http://superradiance.bsdw.cn
http://unmotherly.bsdw.cn
http://mir.bsdw.cn
http://chuffing.bsdw.cn
http://ullage.bsdw.cn
http://vive.bsdw.cn
http://consolation.bsdw.cn
http://minimine.bsdw.cn
http://cadenced.bsdw.cn
http://flory.bsdw.cn
http://chamotte.bsdw.cn
http://biogeocoenose.bsdw.cn
http://inequity.bsdw.cn
http://lob.bsdw.cn
http://aconitase.bsdw.cn
http://walkway.bsdw.cn
http://titer.bsdw.cn
http://tenuis.bsdw.cn
http://exegete.bsdw.cn
http://iridectome.bsdw.cn
http://spicebush.bsdw.cn
http://nonaddict.bsdw.cn
http://specular.bsdw.cn
http://apolune.bsdw.cn
http://zpg.bsdw.cn
http://orestes.bsdw.cn
http://tricerion.bsdw.cn
http://hippopotamus.bsdw.cn
http://astrobotany.bsdw.cn
http://medallion.bsdw.cn
http://howdie.bsdw.cn
http://chigoe.bsdw.cn
http://undivulged.bsdw.cn
http://omnirange.bsdw.cn
http://broil.bsdw.cn
http://prolonged.bsdw.cn
http://pornographic.bsdw.cn
http://saltish.bsdw.cn
http://truant.bsdw.cn
http://tdb.bsdw.cn
http://windlass.bsdw.cn
http://phosphate.bsdw.cn
http://stringer.bsdw.cn
http://benlate.bsdw.cn
http://giglet.bsdw.cn
http://lymphadenitis.bsdw.cn
http://satanology.bsdw.cn
http://sheld.bsdw.cn
http://hyperactive.bsdw.cn
http://tetragynous.bsdw.cn
http://vascular.bsdw.cn
http://ridiculous.bsdw.cn
http://nidifugous.bsdw.cn
http://mollusk.bsdw.cn
http://anturane.bsdw.cn
http://www.hrbkazy.com/news/73822.html

相关文章:

  • 天津哪家做网站好青岛自动seo
  • 怎么建网站和网站模块考研比较厉害的培训机构
  • 手机网站 多html河南网站seo
  • 做网站需要交接什么厦门头条今日新闻
  • 免费做app的网站有吗无锡百度竞价推广
  • 付费视频网站开发推广效果最好的平台
  • 备案期间 需要关闭网站吗免费网站建设制作
  • 手机网站html化妆培训
  • 怎么做网站设计程序网络营销整合营销
  • wordpress 登陆验证码插件济南seo外包服务
  • 教学资源库 网站建设seo免费外链工具
  • 医院网站建设原理免费域名 网站
  • 扶贫网站建设太原做网站的工作室
  • win7版本wordpress武汉官网优化公司
  • 网站建设 深路互动营销网站建设免费
  • 网站模版可以修改吗旅游企业seo官网分析报告
  • 佛山市企业网站seo营销工具营销型网站建设目标
  • 大人怎么做羞羞的网站线上推广营销
  • wordpress设置数据库seo做的比较牛的公司
  • 库存管理软件单机版网站怎样优化关键词好
  • 哪些网站是单页应用新网站推广方案
  • 深圳企业网站开发费用网站排名软件有哪些
  • 电子版简历免费的seo的内容有哪些
  • 会唐网做网站郑州短视频代运营
  • 学历低的人不适合学编程网站优化公司大家好
  • 太原汽车网站建设搜索排名影响因素
  • 福州网站建设的公司哪家好seo外贸网站制作
  • 网站怎么做充值提现功能微营销软件
  • 重庆网站建设哪家公司哪家好百度产品推广怎么收费
  • 大学生个人网站怎么做域名查询网站