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;
    }
相关推荐
1368木林森1 天前
RAG查询改写②【第十篇】:HYDE、StepBack、子问题拆分,高阶改写算法生产落地
人工智能·算法·rag
smj2302_796826521 天前
解决leetcode第3934题最短唯一子数组
数据结构·python·算法·leetcode
NashSKY1 天前
EPnP 算法详解
算法·矩阵分解·多视图几何·射影几何
小O的算法实验室1 天前
2026年SEVC,自适应模因算法+复杂约束条件下多无人机协同任务分配,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
绪风7501 天前
Airtest_Ide
职场和发展
iiiiyu1 天前
面向对象和集合编程题
java·开发语言·前端·数据结构·算法·编程语言
xiaoxiaoxiaolll1 天前
Light首次发表:动量空间穆勒矩阵偏振测量,破解纳米手性结构表征难题
人工智能·算法
变量未定义~1 天前
最长回文子串
数据结构·算法
один but you1 天前
Hash表
缓存·面试·职场和发展
BirdenT1 天前
20260518紫题训练
c++·算法