Skip to content

Commit b63556c

Browse files
committed
Add solution for course schedule
1 parent 30dcf26 commit b63556c

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

Diff for: Course Schedule.js

+97
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,102 @@ var canFinish = function(numCourses, prerequisites) {
4040
var hasCycle = dfs(nodes[i], []);
4141
if (hasCycle) return false;
4242
}
43+
return true;
44+
};
45+
46+
47+
48+
// SOLUTION 2
49+
/**
50+
There are a total of n courses you have to take, labeled from 0 to n - 1.
51+
52+
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]
53+
54+
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
55+
56+
For example:
57+
58+
2, [[1,0]]
59+
There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.
60+
61+
2, [[1,0],[0,1]]
62+
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.
63+
64+
Note:
65+
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
66+
67+
click to show more hints.
68+
69+
Hints:
70+
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.
71+
Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
72+
Topological sort could also be done via BFS.
73+
*/
74+
/**
75+
* @param {number} numCourses
76+
* @param {number[][]} prerequisites
77+
* @return {boolean}
78+
*/
79+
var canFinish = function(numCourses, prerequisites) {
80+
var courses = [],
81+
prereqCounts = [],
82+
temp,
83+
setIter,
84+
i,
85+
j,
86+
k;
87+
88+
for (i = 0; i < numCourses; i++) {
89+
courses.push(new Set());
90+
}
91+
92+
// [1] is [0]'s prerequisite
93+
for (i = 0; i < prerequisites.length; i++) {
94+
courses[prerequisites[i][1]].add(prerequisites[i][0]);
95+
}
96+
97+
for (i = 0; i < numCourses; i++) {
98+
prereqCounts[i] = 0;
99+
}
100+
101+
// count the pre-courses
102+
for (i = 0; i < numCourses; i++) {
103+
temp = Array.from(courses[i]);
104+
// setIter = temp[Symbol.iterator]();
105+
106+
// while(setIter.hasNext()) {
107+
// prereqCounts[setIter.next()]++;
108+
// }
109+
for (j = 0; j < temp.length; j++) {
110+
prereqCounts[temp[j]]++;
111+
}
112+
}
113+
114+
// remove a non-pre course each time
115+
for (i = 0; i < numCourses; i++) {
116+
for (j = 0; j < numCourses; j++) {
117+
if (prereqCounts[j] === 0) {
118+
break;
119+
}
120+
}
121+
122+
// if didn't find a non-pre course
123+
if (j === numCourses) {
124+
return false;
125+
}
126+
prereqCounts[j] = -1;
127+
// decrease courses that post the course
128+
temp = Array.from(courses[j]);
129+
// setIter = temp[Symbol.iterator]();
130+
131+
// while(setIter.hasNext()) {
132+
// prereqCounts[setIter.next()]--;
133+
// }
134+
135+
for (k = 0; k < temp.length; k++) {
136+
prereqCounts[temp[k]]--;
137+
}
138+
}
139+
43140
return true;
44141
};

0 commit comments

Comments
 (0)