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

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

[复制链接]
  • TA的每日心情
    无聊
    2025-1-31 20:04
  • 签到天数: 195 天

    [LV.7]常住居民III

    745

    主题

    6465

    回帖

    7159

    积分

    管理员

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

    积分
    7159

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

    发表于 2025-2-9 13:15:44 | 显示全部楼层

    https://leetcode.cn/problems/find-the-distance-value-between-two-arrays/submissions/597896241/
    秒了

    const findMin = (arr, target) => {
      let left = 0,
        right = arr.length - 1;
      let ans = -1;
      while (left <= right) {
        const mid = Math.floor((right + left) / 2);
        const char = arr[mid];
        if (char < target) {
          left = mid + 1;
          ans = mid;
        } else {
          right = mid - 1;
        }
      }
      return ans;
    };
    const findMax = (arr, target) => {
      let left = 0,
        right = arr.length - 1;
      let ans = -1;
      while (left <= right) {
        const mid = Math.floor((right + left) / 2);
        const char = arr[mid];
        if (char <= target) {
          left = mid + 1;
        } else {
          right = mid - 1;
          ans = mid;
        }
      }
      return ans;
    };
    var findTheDistanceValue = function (arr1, arr2, d) {
      arr2=arr2.sort((a,b)=>a-b)
      let ans=0;
    for (let index = 0; index < arr1.length; index++) {
      const num = arr1[index];
      const min = findMin(arr2, num);
      const max = findMax(arr2, num);
      const sep = Math.min(num - (arr2[min]??num-d-1), (arr2[max]??num+d+1) - num,Math.abs( (arr2[max-1]??num+d+1) - num));
      if(sep>d){
          ans++
      }
    }
    return ans
    };
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

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

    使用道具 举报

  • TA的每日心情
    无聊
    2025-1-31 20:04
  • 签到天数: 195 天

    [LV.7]常住居民III

    745

    主题

    6465

    回帖

    7159

    积分

    管理员

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

    积分
    7159

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

    发表于 2025-2-10 22:21:41 | 显示全部楼层

    https://leetcode.cn/problems/find-if-path-exists-in-graph/submissions/598303765/

    var validPath = function (n, edges, source, destination) {
      const arr = new Array(n).fill(0).map(() => []);
      const visit = new Array(n).fill(false);
      for (const [a, b] of edges) {
        arr[a].push(b);
        arr[b].push(a);
      }
      const dfs = (pos) => {
        if(pos==destination){
            return true
        }
        if (visit[pos]) {
          return false;
        }
        visit[pos] = true;
        let ans = false;
        for (let index = 0; index < arr[pos].length && ans == false; index++) {
          const item = arr[pos][index];
          ans = dfs(item);
        }
        return ans;
      };
      const ans=dfs(source)
      return ans
    };
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

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

    使用道具 举报

  • TA的每日心情
    无聊
    2025-1-31 20:04
  • 签到天数: 195 天

    [LV.7]常住居民III

    745

    主题

    6465

    回帖

    7159

    积分

    管理员

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

    积分
    7159

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

    发表于 2025-2-10 22:30:17 | 显示全部楼层

    https://leetcode.cn/problems/all-paths-from-source-to-target/submissions/598307446/

    var allPathsSourceTarget = function (graph) {
    
      const ans = [];
      const dfs = (pos, arr) => {
        arr.push(pos);
        if (pos == graph.length - 1) {
          ans.push([...arr]);
          arr.pop();
          return;
        }
        for (let index = 0; index < graph[pos].length ; index++) {
          const item = graph[pos][index];
          dfs(item,arr);
        }
        arr.pop();
        return;
      };
      dfs(0, []);
      return ans;
    };
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

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

    使用道具 举报

  • TA的每日心情
    无聊
    2025-1-31 20:04
  • 签到天数: 195 天

    [LV.7]常住居民III

    745

    主题

    6465

    回帖

    7159

    积分

    管理员

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

    积分
    7159

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

    发表于 2025-2-10 22:36:05 | 显示全部楼层

    https://leetcode.cn/problems/keys-and-rooms/submissions/598309416/
    过`

    var canVisitAllRooms = function (rooms) {
      const n = rooms.length;
      const visit = new Array(n).fill(false);
      let all=0;
      const dfs = (pos) => {
        if (visit[pos]) {
          return false;
        }
        visit[pos] = true;
        all++
        for (let index = 0; index < rooms[pos].length; index++) {
          const item = rooms[pos][index];
          dfs(item);
        }
      };
       dfs(0);
      return rooms.length==all;
    };
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

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

    使用道具 举报

  • TA的每日心情
    无聊
    2025-1-31 20:04
  • 签到天数: 195 天

    [LV.7]常住居民III

    745

    主题

    6465

    回帖

    7159

    积分

    管理员

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

    积分
    7159

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

    发表于 2025-2-10 23:22:02 | 显示全部楼层

    https://leetcode.cn/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph/submissions/598323095/
    过了

    var countPairs = function (n, edges) {
      const arr = new Array(n).fill(0).map(() => []);
      const visit = new Array(n).fill(false);
      for (const [a, b] of edges) {
        arr[a].push(b);
        arr[b].push(a);
      }
      const dfs = (pos, sum) => {
        if (visit[pos]) {
          return 0;
        }
        visit[pos] = true;
        sum = 1;
        for (let index = 0; index < arr[pos].length; index++) {
          const item = arr[pos][index];
          sum += dfs(item, sum);
        }
        return sum;
      };
      const list = [];
      let ret = 0;
      let sum = 0;
      for (let index = 0; index < n; index++) {
        const ans = dfs(index);
        if (ans !== 0) {
          list.push(ans);
          if (list.length != 1) {
            ret+=ans*sum
          }
          sum += ans;
        }
      }
    
      return ret
    };
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

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

    使用道具 举报

  • TA的每日心情
    无聊
    2025-1-31 20:04
  • 签到天数: 195 天

    [LV.7]常住居民III

    745

    主题

    6465

    回帖

    7159

    积分

    管理员

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

    积分
    7159

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

    发表于 2025-2-11 11:39:27 | 显示全部楼层

    https://leetcode.cn/problems/minimum-score-of-a-path-between-two-cities/submissions/598406384/
    打测例硬凹过去了

    var minScore = function (n, roads) {
      n = n + 1;
      const arr = new Array(n).fill(0).map(() => []);
      for (const [a, b, c] of roads) {
        arr[a].push([b, c]);
        arr[b].push([a, c]);
      }
      const dfs = (pos, sum, visit) => {
    
        if (visit[pos] !== -1) {
          visit[pos] = Math.min(visit[pos], sum);
          return visit[pos];
        }
        let ans = sum;
        visit[pos]=sum
        for (let index = 0; index < arr[pos].length; index++) {
          const [target, value] = arr[pos][index];
          ans = Math.min(ans, dfs(target, Math.min(sum, value), visit));
        }
        visit[pos] = ans;
        return ans;
      };
      return dfs(1, Number.MAX_SAFE_INTEGER, new Array(n).fill(-1));
    };
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

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

    使用道具 举报

    发表回复

    本版积分规则

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