首页 > 代码库 > 安排课程

安排课程

技术分享
public class Solution {
    /**
     * @param numCourses a total of n courses
     * @param prerequisites a list of prerequisite pairs
     * @return the course order
     */
    private class Node {
        int label;
        ArrayList<Integer> pre;
        /*
        public node(int la; ArrayList<Integer> prerequist) {
            label = la;
            pre = pre;
        }
        */
    }
    public int[] findOrder(int numCourses, int[][] prerequisites) {
        // Write your code here
        if (prerequisites == null || prerequisites.length == 0 || prerequisites[0].length == 0) {
            return new int[0];
        }
        int[] result = new int[numCourses];
        ArrayList<int> order = new ArrayList<int>();
        Map<int node, ArrayList<int> request> indegree = getIndegree(numCourses,prerequisites);
        Queue<int> queue = new LinkedList<>();
        for (int i = 0; i < numCourses; i++) {
            if (indegree.get(i) == 0) {
                queue.offer(i);
                order.add(i);
            }
        }
        while (!queue.isEmpty()) {
            int node = queue.poll;
            for (int i = 0; i < prerequisites.length; i++) {
                if(prerequisites[i][1] = node) {
                    prerequisites[i][0]
                }
            }
        }
 
        
    }
    private Map<int node, ArrayList<int> request> getIndegree (int numCourses, int[][] prerequisites)
    {
        Map<int node, ArrayList<int> request> indegree = new HashMap();
        //init map
        for (int j = 0; j < numCourses; j++) {
            indegree.put(j, new ArrayLisr<int>());
        }
        for (int i = 0; i < prerequisites.length; i++) {
            indegree.put(prerequisites[i][0], indegree.get(prerequisites[i][0]).add());
        }
    }
}
View Code

写不出来

安排课程