Course Schedule I& II leetcode

Course Schedule

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

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?ui

For example:code

2, [[1,0]] There are a total of 2 courses to take. To take course 1
you should have finished course 0. So it is possible.排序

2, [[1,0],[0,1]] 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.ip

思路

先修課程是拓撲排序的經典應用, 這裏至關於找有向圖是否有環, 若是有環的話拓撲排序能遍歷到的節點將少於圖的節點. 這裏咱們創建一個圖, 用一個數組記錄每一個節點的入度. 對圖進行拓撲排序it

複雜度

時間O(V+E) 空間 O(V)sed

代碼

public boolean canFinish(int numCourses, int[][] prerequisites) {
    if (prerequisites == null || prerequisites.length == 0 || prerequisites[0].length == 0) {
        return true;
    }
    //記錄入度
    int[] indgree = new int[numCourses];
    //記錄有向圖的指向節點
    ArrayList[] graph = new ArrayList[numCourses];
    for (int i = 0; i < numCourses; i++) {
        graph[i] = new ArrayList<Integer>();
    }
    //寫入有向圖的next節點
    for (int i = 0; i < prerequisites.length; i++) {
        graph[prerequisites[i][1]].add(prerequisites[i][0]);
        indgree[prerequisites[i][0]]++;
    }
    Queue<Integer> queue = new LinkedList<Integer>();
    for(int i = 0; i < indgree.length; i++){
        if(indgree[i] == 0){
            queue.add(i);
        }
    }
    int count = 0;
    while (!queue.isEmpty()) {
        int cur = queue.poll();
        count++;
        ArrayList<Integer> list = graph[cur];
        for (Integer tem : list) {
            indgree[tem]--;
            if (indgree[tem] == 0) {
                queue.offer(tem);
            }
        }
    }
    return count == numCourses;
}

Course Schedule II

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, return the ordering of courses you should take to finish all

courses.List

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty

array.遍歷

For example:

2, [[1,0]]
There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1]

4, [[1,0],[2,0],[3,1],[3,2]]
There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should

be taken after you finished course 0. So one correct course order is
[0,1,2,3]. Another correct ordering is[0,2,1,3].

思路

與I相同 只是要打印出結果

複雜度

時間O(V+E) 空間 O(V)

代碼

public int[] findOrder(int numCourses, int[][] prerequisites) {
        int[] res = new int[numCourses];
        //記錄入度
        int[] indgree = new int[numCourses];
        //記錄有向圖的指向節點
        ArrayList[] graph = new ArrayList[numCourses];
        for (int i = 0; i < numCourses; i++) {
            graph[i] = new ArrayList<Integer>();
        }
        //寫入有向圖的next節點
        for (int i = 0; i < prerequisites.length; i++) {
            graph[prerequisites[i][1]].add(prerequisites[i][0]);
            indgree[prerequisites[i][0]]++;
        }
        Queue<Integer> queue = new LinkedList<Integer>();
        for(int i = 0; i < indgree.length; i++){
            if(indgree[i] == 0){
                queue.add(i);
            }
        }
        int count = 0;
        while (!queue.isEmpty()) {
            int cur = queue.poll();
            res[count++] = cur;
            ArrayList<Integer> list = graph[cur];
            for (Integer tem : list) {
                indgree[tem]--;
                if (indgree[tem] == 0) {
                    queue.offer(tem);
                }
            }
        }
        return count != numCourses ? new int[0] : res;
    }
相關文章
相關標籤/搜索