学习笔记学习笔记
主站博客
面试
前端
开发工具
Cesium
GitHub
主站博客
面试
前端
开发工具
Cesium
GitHub
  • 面试
  • 算法

    • hash哈希

      • lc-1-简-两数之和
    • top K

      • lc-215-数组中的第K个最大元素
      • lc-347-中-前 K 个高频元素
      • lc-LCR159-简-库存管理 III
    • 二叉树

      • lc-101-简-对称二叉树
      • lc-102-中-二叉树的层序遍历
      • lc-104-简-二叉树的最大深度
      • lc-105-中-从前序与中序遍历序列构造二叉树
      • lc-114-中-二叉树展开为链表
      • lc-226-简-翻转二叉树
      • lc-236-中-二叉树的最近公共祖先
    • 动态规划

      • lc-1277-中-统计全为 1 的正方形子矩阵
      • lc-221-中-最大正方形
      • lc-62-中-不同路径
      • lc-70-简-爬楼梯
      • lc-72-中-编辑距离
      • lc-746-简-使用最小花费爬楼梯
      • 背包-01背包
      • 背包-完全背包
    • 原地哈希

      • lc-33-中-搜索旋转排序数组
      • lc-442-中-数组中重复的数据
      • lc-448-简-找到所有数组中消失的数字
    • 图

      • lc-207-中-课程表
      • lc-997- 简-找到小镇的法官
    • 待分类

      • lc-11-中-盛最多水的容器
      • lc-121-简-买卖股票的最佳时机
      • lc-128-中-最长连续序列
      • lc-136-简-只出现一次的数字
      • lc-139-中-单词拆分
      • lc-152-中-乘积最大子数组
      • lc-20-简-有效的括号
      • lc-200-中-岛屿数量
      • lc-22-中-括号生成
      • lc-279-中-完全平方数
      • lc-31-中-下一个排列
      • lc-322-中-零钱兑换
      • lc-34-中-在排序数组中查找元素的第一个和最后一个位置
      • lc-39-中-组合总和
      • lc-416-中-分割等和子集
      • lc-42-难-接雨水
      • lc-437-中-路径总和 III
      • lc-438-中-找到字符串中所有字母异位词
      • lc-49-中-字母异位词分组
      • lc-53-中-最大子数组和
      • lc-55-中-跳跃游戏
      • lc-56-中-合并区间
      • lc-560-中-和为 K 的子数组
      • lc-647-中-回文子串
      • lc-79-中-单词搜索
    • 排序

      • 归并排序

        • 归并排序
      • 快排
      • 排序+双指针

        • lc- 524-中-通过删除字母匹配到字典里最长单词
        • lc-15-中-三数之和
        • lc-16-中-最接近的三数之和
        • lc-283-简-移动零
        • lc-3-中-无重复字符的最长子串
        • lc-75-中-颜色分类
      • 计数排序
    • 算法工具库js
    • 递归+回溯

      • lc-17-中-电话号码的字母组合
      • lc-77-中-组合
      • LCR-083-中-全排列-不重复
      • LCR-084-中-全排列-重复
    • 链表

      • lc-142-中-环形链表 II
      • lc-148-中-排序链表
      • lc-160-简-相交链表
      • lc-19-中-删除链表的倒数第 N 个结点
      • lc-287-中-寻找重复数
  • 手撕代码

    • 柯里化 Curring
    • h5 Evnet详解
    • promiseify化
    • script标签详解
    • vue-SSR服务端渲染
    • vue双向绑定
    • vue核心原理全解
    • 路由的实现
    • 原型链
    • 变量提升
    • 宏任务、微任务
    • 左右两栏布局
    • 异步
    • 微前端
    • 实现js的filter函数
    • 实现promise
    • 捕获、冒泡
    • js中new的本质
    • Object.xxx
    • this
    • 反射Reflect,、代理Proxy
    • 基础类型
    • 深拷贝-浅拷贝
    • 继承
    • 跨域
    • 闭包
    • 防抖-节流

lc-207-中-课程表

/*
https://leetcode.cn/problems/course-schedule/description/
207. 课程表
你这个学期必须选修 numCourses 门课程,记为 0 到 numCourses - 1 。

在选修某些课程之前需要一些先修课程。 先修课程按数组 prerequisites 给出,其中 prerequisites[i] = [ai, bi] ,表示如果要学习课程 ai 则 必须 先学习课程  bi 。

例如,先修课程对 [0, 1] 表示:想要学习课程 0 ,你需要先完成课程 1 。
请你判断是否可能完成所有课程的学习?如果可以,返回 true ;否则,返回 false 。
*/


/*
 思路是清晰的,试了很多次才写完,特殊情况比较多,考虑的不够周全
*/
var canFinish = function(numCourses, prerequisites) {
  // let courseGraph = Array.from({length: numCourses}, (index)=>({inDegree: 0, index, outArray: []}))

  let courseGraph = new Array(numCourses)
  for (let i = 0; i < courseGraph.length; i++) {
    courseGraph[i] = {inDegree: 0, index: i, outArray: []}  // 这里加上index, 方便debug
    
  }

  let hasCircleRequire= false
  prerequisites.forEach(([a, b], index) => {
    if (a === b) {
      // 排除 [1,1] 的情况
      hasCircleRequire = true
    }
    courseGraph[a].inDegree++
    courseGraph[b].outArray.push(a)
  });
  if (hasCircleRequire) {
    return false
  }


  let lastFreeCouseCount = 0
  let freeCourseArr = []
  while (courseGraph.length && freeCourseArr.length !== numCourses) {
    freeCourseArr=[] // 新循环进来清空
    courseGraph.forEach(course => {
      if (course.inDegree<=0) {
        freeCourseArr.push(course)
      }
    });
    if (freeCourseArr.length === 0) {
      return false
    }
    if (freeCourseArr.length === lastFreeCouseCount) {
      // 没有找到更多的free课程
      return false
    }
    lastFreeCouseCount = freeCourseArr.length


    freeCourseArr.forEach(freeCourse => {
      freeCourse.outArray.forEach(outIndex=>{
        courseGraph[outIndex].inDegree --
      })
      freeCourse.outArray = [ ] // 需要清空,否则会持续减
    });
  }
  return true
};


console.log(canFinish(2, [[1,0]])); // true
console.log(canFinish(2, [[1,0],[0,1]])); // false
console.log(canFinish(20,[[0,10],[3,18],[5,5],[6,11],[11,14],[13,1],[15,1],[17,4]])); // false
console.log(canFinish(3, [[2,0],[2,1]])); // true
console.log(canFinish(5, [[1,4],[2,4],[3,1],[3,2]])); // true
console.log(canFinish(2, [[1,0],[0,1]])); // false
console.log(canFinish(4, [[2,0],[1,0],[3,1],[3,2],[1,3]])); // false
在 GitHub 上编辑此页
上次更新:
贡献者: 国wei
Next
lc-997- 简-找到小镇的法官