53、图论-课程表

思路:

 其实就是图的拓扑排序,我们可以构建一个图形结构,比如[0,1]表示1->0,对于0来说入度为+1。

遍历结束后,从入度为0的开始遍历。引文只有入度为0的节点没有先决条件。然后依次减少1。直到所有节点入度都为0.然后记录下来count和需要学习课程数相比如果相等表示可以。不相等表示存在环。

class Solution {
   public static class Node {
        public int name;
        public int in;
        public ArrayList<Node> nexts;

        public Node(int n) {
            name = n;
            in = 0;
            nexts = new ArrayList<>();
        }
    }

    public static boolean canFinish(int numCourses, int[][] prerequisites) {
        if (prerequisites == null || prerequisites.length == 0) {
            return true;
        }
        Map<Integer, Node> nodes = new HashMap<>();
        for (int[] arr : prerequisites) {
            //目标课程编号
            int to = arr[0];
            //前驱课程编号
            int from = arr[1];
            if (!nodes.containsKey(to)) {
                nodes.put(to, new Node(to));
            }
            if (!nodes.containsKey(from)) {
                nodes.put(from, new Node(from));
            }
            //获取目标课程 节点
            Node t = nodes.get(to);
            Node f = nodes.get(from);
            //表示前驱课程指向目标课程  就是图的有向指标
            f.nexts.add(t);
            //目标课程入度++
            t.in++;
        }
        int needPrerequisiteNums = nodes.size();
        //建立一个入度为0 队列 表示这个是起始节点
        Queue<Node> zeroInQueue = new LinkedList<>();
        for (Node node : nodes.values()) {
            if (node.in == 0) {
                zeroInQueue.add(node);
            }
        }
        int count = 0;
        //拓扑排序  一次减少入度  如果count!=needPrerequisiteNums 说明有环,循环依赖 
        while (!zeroInQueue.isEmpty()) {
            Node cur = zeroInQueue.poll();
            count++;
            for (Node next : cur.nexts) {
                if (--next.in == 0) {
                    zeroInQueue.add(next);
                }
            }
        }
        return count == needPrerequisiteNums;
    }
}

相关推荐

  1. 】Leetcode 207. 课程表【中等】

    2024-04-23 12:54:02       13 阅读
  2. 【LeetCode热题100】【课程表

    2024-04-23 12:54:02       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-23 12:54:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-23 12:54:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-23 12:54:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-23 12:54:02       20 阅读

热门阅读

  1. SQL server详细使用教程

    2024-04-23 12:54:02       15 阅读
  2. 数据结构——6.4 图的应用

    2024-04-23 12:54:02       17 阅读
  3. Ajax技术是啥?在web开发中有啥用?

    2024-04-23 12:54:02       29 阅读
  4. Apache Spark 的基本概念和在大数据分析中的应用

    2024-04-23 12:54:02       29 阅读
  5. 使用OpenCV计算滑块缺口

    2024-04-23 12:54:02       15 阅读
  6. .NET 高级开发人员面试常见问题及解答

    2024-04-23 12:54:02       20 阅读
  7. NLP预训练模型-GPT-3

    2024-04-23 12:54:02       15 阅读
  8. .NET WinForm开放中的 窗体的 Designer.cs作用

    2024-04-23 12:54:02       21 阅读
  9. Rx.Net 第四章

    2024-04-23 12:54:02       16 阅读
  10. [python] __setitem__与__getitem__的使用

    2024-04-23 12:54:02       45 阅读
  11. git 常用命令

    2024-04-23 12:54:02       41 阅读
  12. Elasticsearch与IK分词器:深度解析与实战应用

    2024-04-23 12:54:02       31 阅读