Course Schedule ii

来源:互联网 发布:485接口数据交换 编辑:程序博客网 时间:2024/06/01 08:07

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.

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].

java

class Solution {    public int[] findOrder(int numCourses, int[][] prerequisites) {        int[] degree = new int[numCourses];        List[] edges = new ArrayList[numCourses];        for (int i = 0; i < numCourses; i++) {            edges[i] = new ArrayList<Integer>();        }        for (int i = 0; i < prerequisites.length; i++) {            degree[prerequisites[i][0]]++;            edges[prerequisites[i][1]].add(prerequisites[i][0]);        }        Queue<Integer> queue = new LinkedList<>();        int[] array = new int[numCourses];        for (int i = 0; i < numCourses; i++) {            if (degree[i] == 0) {                queue.offer(i);            }        }        int count = 0;        while (!queue.isEmpty()) {            int element = queue.poll();            array[count++] = element;            List list = edges[element];            for (int i = 0; i < list.size(); i++) {                degree[(int)list.get(i)]--;                if (degree[(int)list.get(i)] == 0) {                    queue.offer((int)list.get(i));                }            }        }        if (count == numCourses) {            return array;        } else {            return new int[0];        }    }}

python

from Queue import Queueclass Solution:    """    @param: numCourses: a total of n courses    @param: prerequisites: a list of prerequisite pairs    @return: the course order    """    def findOrder(self, numCourses, prerequisites):        # write your code here        degree = []        edges = []        for i in range(numCourses):            edges.append([])            degree.append(0)        for ele in prerequisites:            degree[ele[0]] += 1            edges[ele[1]].append(ele[0])        queue = Queue()        for i in range(numCourses):            if degree[i] is 0:                queue.put(i)        arr = []        while not queue.empty():            ele = queue.get()            arr.append(ele)            for node in edges[ele]:                degree[node] -= 1                if degree[node] is 0:                    queue.put(node)        if len(arr) == numCourses:            return arr        return []            



原创粉丝点击