207. Course Schedule

解法一 DFS

思路

Using topological sort to solve this question. Topological sort is a linear order of vertices which for every edge uv from u to v, u always comes before v. As long as there is a cycle detected, which means there exists an edge uv, v comes before u, so it is not a topological sort.
To actual solve this problem, we may first construct a graph for the prerequisite courses. We may use ArrayList<Integer>[] array to build the graph. For each course, we add all the illegible courses we can take after it.
Then for each course, we do a dfs to search the following courses. We lable each of the vertex we currently visit in this round as visiting. If the vertex we currently visit has been labled as visiting, then there will be a cycle. If a vertex has no out-going edge, we label it as visited. The exit for the dfs recursion is if we reach a vertex which is visiting, we return true cuz we find a cycle; if the vertex is visited, we return false because it has no out going edges.

代码
class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        // Construct a graph for course
        ArrayList<Integer>[] graph = new ArrayList[numCourses];
        for (int i = 0; i < numCourses; i++) {
            graph[i] = new ArrayList<Integer>(); // To add all illegible courses in.
        }
        for (int[] pre : prerequisites) {
            graph[pre[1]].add(pre[0]);
        }

        int[] visit = new int[numCourses];

        for (int i = 0; i <numCourses; i++) {
            if (hasCycle(i, visit, graph)) return false;
        }
        return true;
    }

    // DFS to detect a cycle
    // 1 = visiting; 2 = visited
    private boolean hasCycle(int index, int[] visit, ArrayList<Integer>[] graph) {
        if (visit[index] == 1) {
            return true;
        } else if (visit[index] == 2) {
            return false;
        }

        visit[index] = 1;
        for (int i = 0; i < graph[index].size(); i++) {
            int neighbor = graph[index].get(i);
            if (hasCycle(neighbor, visit, graph)) {
                return true;
            }
        }
        visit[index] = 2;
        return false;
    }
}
复杂度分析
  • 时间复杂度
    The time complexity for topological sort is O(|V| + |E|).
  • 空间复杂度
    O(|V||E|)?

解法二 BFS

思路
代码
复杂度分析
  • 时间复杂度
    • 最好情况
    • 最坏情况
    • 平均情况
  • 空间复杂度

Takeaway

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!