LeetCode【207】课程表

题目:

思路:

https://www.jianshu.com/p/25868371ddfc/

代码:

java 复制代码
public boolean canFinish(int numCourses, int[][] prerequisites) {
        // 入度
        int[] indegress = new int[numCourses];

        // 每个点对应的边,出边
        Map<Integer, List<Integer>> adjacency = new HashMap<>();

        Queue<Integer> queue = new LinkedList<>();

        for (int i = 0; i < numCourses; i++) {
            adjacency.put(i, new ArrayList<>());
        }

        for (int[] cp : prerequisites) {
            indegress[cp[0]]++;
            adjacency.get(cp[1]).add(cp[0]);
        }

        for (int i = 0; i < numCourses; i++) {
            if (indegress[i] == 0) {
                queue.offer(i);
            }
        }

        // BFS
        while (!queue.isEmpty()) {
            Integer pre = queue.poll();
            numCourses--;
            for (int cur : adjacency.get(pre)) {
                if (--indegress[cur] == 0) {
                    queue.offer(cur);
                }
            }
        }

        return numCourses == 0;
    }
相关推荐
Raink老师8 小时前
【AI面试临阵磨枪-62】设计基于 RAG 的内部知识库问答平台(多租户、权限、文件上传、实时更新)
人工智能·面试·职场和发展
smj2302_796826529 小时前
解决leetcode第3943题递增后的数对数量
数据结构·python·算法·leetcode
炽烈小老头10 小时前
【每天学习一点算法 2026/05/25】矩阵中的最长递增路径
学习·算法·矩阵
我爱cope10 小时前
【Agent智能体6 | 智能体AI评估】
人工智能·职场和发展
叁散10 小时前
实验报告:5G 仿真环境与基本链路模拟
算法
从负无穷开始的三次元代码生活11 小时前
算法零碎灵感点分享
算法
染指111011 小时前
9.LangChain框架(实现RAG)
数据库·人工智能·算法·机器学习·ai·大模型
我爱cope11 小时前
【Agent智能体5 | 任务分解:识别工作流中的步骤】
人工智能·职场和发展
大数据三康11 小时前
在spyder进行的遗传算法练习
开发语言·python·算法
Gene_202211 小时前
轮式底盘的微分平坦
算法