上一主题 下一主题
ScriptCat,新一代的脚本管理器脚本站,与全世界分享你的用户脚本油猴脚本开发指南教程目录
返回列表 发新帖
楼主: 李恒道 - 

【当前排名48762】挑战leetcode进入前1w名

[复制链接]
  • TA的每日心情
    无聊
    3 天前
  • 签到天数: 195 天

    [LV.7]常住居民III

    740

    主题

    6347

    回帖

    7072

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    7072

    荣誉开发者喜迎中秋油中2周年生态建设者

    发表于 1 小时前 | 显示全部楼层

    https://leetcode.cn/problems/maximum-fruits-harvested-after-at-most-k-steps/submissions/596428450/
    2062分
    刺激

    var maxTotalFruits = function (fruits, startPos, k) {
      const cache = new Map();
      for (let index = 0; index < fruits.length; index++) {
        const fruit = fruits[index];
        cache.set(fruit[0], fruit[1]);
      }
      const nextArr = new Array(k + 1);
      const preArr = new Array(k + 1);
      let ans = 0;
      preArr[0] = 0;
      nextArr[0] = 0;
      for (let index = 1; index <= k; index++) {
        nextArr[index] =
          (nextArr[index - 1] ?? 0) + (cache.get(startPos + index) ?? 0);
        preArr[index] =
          (preArr[index - 1] ?? 0) + (cache.get(startPos - index) ?? 0);
      }
      ans = Math.max(nextArr[k], preArr[k]);
      let step = 1;
      while (step * 2 < k) {
        ans = Math.max(ans, preArr[step] + nextArr[k - 2 * step]);
        ans = Math.max(ans, nextArr[step] + preArr[k - 2 * step]);
        step++;
      }
      return ans+(cache.get(startPos)??0);
    };
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.com/a/lihengdao666
    回复
    订阅

    使用道具 举报

    发表回复

    本版积分规则

    快速回复 返回顶部 返回列表