-
Notifications
You must be signed in to change notification settings - Fork 0
/
207_CourseSchedule.java
113 lines (96 loc) · 4.18 KB
/
207_CourseSchedule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 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.
// click to show more hints.
// Hints:
// This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
// Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
// Topological sort could also be done via BFS.
//BFS
public class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
int cnt = numCourses;
Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
Queue<Integer> queue = new LinkedList<>();
int[] indegree = new int[numCourses];
for(int i=0; i<numCourses; i++) {
map.put(i, new ArrayList<Integer>());
}
for(int i=0; i<prerequisites.length; i++) {
map.get(prerequisites[i][0]).add(prerequisites[i][1]);
indegree[prerequisites[i][1]]++;
}
for(int i=0; i<numCourses; i++) {
if(indegree[i] == 0) {
queue.offer(i);
}
}
while(!queue.isEmpty()) {
int current = queue.poll();
for(int i : map.get(current)) {
if(--indegree[i] == 0) queue.offer(i);
}
cnt--;
}
return cnt == 0;
}
}
//dfs hashmap tle了。。。why
public class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
ArrayList[] graph = new ArrayList[numCourses];
for(int i=0;i<numCourses;i++)
graph[i] = new ArrayList();
boolean[] visited = new boolean[numCourses];
for(int i=0; i<prerequisites.length;i++){
graph[prerequisites[i][1]].add(prerequisites[i][0]);
}
for(int i=0; i<numCourses; i++){
if(!dfs(graph,visited,i))
return false;
}
return true;
}
private boolean dfs(ArrayList[] graph, boolean[] visited, int course){
if(visited[course])
return false;
else
visited[course] = true;;
for(int i=0; i<graph[course].size();i++){
if(!dfs(graph,visited,(int)graph[course].get(i)))
return false;
}
visited[course] = false;
return true;
}
// public boolean canFinish(int numCourses, int[][] prerequisites) {
// Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
// for(int i=0; i<numCourses; i++) {
// map.put(i, new ArrayList<Integer>());
// }
// for(int i=0; i<prerequisites.length; i++) {
// map.get(prerequisites[i][1]).add(prerequisites[i][0]);
// }
// boolean[] visited = new boolean[numCourses];
// for(int i=0; i<numCourses; i++) {
// if(!helper(map, visited, i)) return false;
// }
// return true;
// }
// public boolean helper(Map<Integer, List<Integer>> map, boolean[] visited, int i) {
// if(visited[i]) return false;
// else visited[i] = true;
// for(int j=0; j<map.get(i).size(); j++) {
// if(!helper(map, visited, map.get(i).get(j))) return false;
// }
// visited[i] = false;
// return true;
// }
}