207. Course Schedule

探讨如何通过广度优先搜索(BFS)解决课程依赖问题,判断在给定的课程依赖关系下是否能够完成所有课程的学习。文章详细解释了如何构建图和计算入度,以及使用队列进行节点处理的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

There are a total of n courses you have to take, labeled from 0 to n-1.

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]

Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?

Example 1:

Input: 2, [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.

Example 2:

Input: 2, [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0, and to take course 0 you should
also have finished course 1. So it is impossible.

Note:

  • The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  • You may assume that there are no duplicate edges in the input prerequisites.
    Problem URL

Solution

给n个课程,以及课程之间的依赖关系的数组,判断在这些依赖之下能否take 所有的课程。本质上是一个有向图连通性的问题。

We use BFS to solve this problem. First, build a two-dimensional array graph to store edges. And an integer array to store indegree of each node(courses).

Then using a queue to store all nodes which do not have indegree. It means that they are prerequest courses. Poll them from the queue and count ++, then minus corresponding indegree. If indegree is 0, it means it could be offered in the queue.

Finally, if the count is equal to num of courses, that means true.

Code
class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        int[][] graph = new int[numCourses][numCourses];
        int[] indegree = new int[numCourses];
        
        for (int i = 0; i < prerequisites.length; i++){
            int pre = prerequisites[i][1];
            int cur = prerequisites[i][0];
            if (graph[pre][cur] == 0){
                indegree[cur]++;//for duplicate case
            }
            graph[pre][cur] = 1;
        }
        
        int count = 0;
        Queue<Integer> queue = new LinkedList<>();
        for (int i = 0; i < numCourses; i++){
            if (indegree[i] == 0){//no dependency, no indegree edge
                queue.offer(i);
            }
        }
        
        while (!queue.isEmpty()){
            int pre = queue.poll();
            count++;
            for (int i = 0; i < numCourses; i++){
                if (graph[pre][i] == 1){
                    indegree[i]--;
                    if (indegree[i] == 0){
                        queue.offer(i);
                    }
                }
            }
        }
        return count == numCourses;
    }
}

Time Complexity: O(p + n)
Space Complexity: O(n^2)


Review
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值