2015年9月15日星期二

Course Schedule

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?
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 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.
Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.

public class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        HashMap<Integer, List<Integer>> edgesMap = new HashMap<>();
        int[] inDegree = new int[numCourses];
        for (int prereq[] : prerequisites) {
            int to = prereq[0];
            int from = prereq[1];
            List<Integer> edge = edgesMap.get(from);
            if (edge == null) {
                edge = new ArrayList<Integer>();
                edgesMap.put(from, edge);
            }
            edge.add(to);
            inDegree[to]++;
        }
        int count = 0;
        LinkedList<Integer> queue = new LinkedList<Integer>();
        for (int i = 0; i < numCourses; i++) {
            if (inDegree[i] == 0) {
                queue.add(i);
            }
        }
        while (!queue.isEmpty()) {
            count++;
            int from = queue.pop();
            List<Integer> edges = edgesMap.get(from);
            if (edges != null) {
                for (int to : edges) {
                    inDegree[to]--;
                    if (inDegree[to] == 0) {
                        queue.offer(to);
                    }
                }
            }
        }
        return count == numCourses;
    }
}

==========

public class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        HashMap<Integer, List<Integer>> edgesMap = new HashMap<>();
        for (int prereq[] : prerequisites) {
            int to = prereq[0];
            int from = prereq[1];
            List<Integer> edge = edgesMap.get(from);
            if (edge == null) {
                edge = new ArrayList<Integer>();
                edgesMap.put(from, edge);
            }
            edge.add(to);
        }
        boolean visited[] = new boolean[numCourses];
        for (int i = 0; i < numCourses; i++) {
            if (!dfs(edgesMap, i, visited)) {
                return false;
            }
        }      
        return true;
    }
   
    private boolean dfs(HashMap<Integer, List<Integer>> edgesMap, int v, boolean[] visited) {
        if (visited[v]) {
            return false;
        }
        visited[v] = true;
        List<Integer> tos = edgesMap.get(v);
        if (tos != null) {
            for (int to : tos) {
                if (!dfs(edgesMap, to, visited)) {
                    return false;
                }
            }
        }
        visited[v] = false;
        return true;
    }
}

没有评论:

发表评论