forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0207-course-schedule.js
204 lines (153 loc) · 5.03 KB
/
0207-course-schedule.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* https://leetcode.com/problems/course-schedule/
* Time O((V)^2 + E) | Space O(V + E)
* @param {number} numCourses
* @param {number[][]} prerequisites
* @return {boolean}
*/
var canFinish = function(numCourses, prerequisites) {
const { graph, path } = buildGraph(numCourses, prerequisites);
return hasPath(numCourses, graph, path);
}
var initGraph = (numCourses) => ({
graph: new Array(numCourses).fill().map(() => []),
path: new Array(numCourses).fill(false)
})
var buildGraph = (numCourses, prerequisites) => {
const { graph, path } = initGraph(numCourses);
for (const [ src, dst ] of prerequisites) {
const neighbors = (graph[dst] || []);
neighbors.push(src);
graph[dst] = neighbors;
}
return { graph, path };
}
var hasPath = (numCourses, graph, path) => {
for (let course = 0; course < numCourses; course++) {
if (isCyclic(course, graph, path)) return false;
}
return true;
}
var isCyclic = (currCourse, graph, path) => {
const hasSeen = path[currCourse]
if (hasSeen) return true
const isMissingNext = !(currCourse in graph)
if (isMissingNext) return false;
return backTrack(currCourse, graph, path);
}
var backTrack = (currCourse, graph, path) => {
path[currCourse] = true;
const _hasCycle = hasCycle(currCourse, graph, path)
path[currCourse] = false;
return _hasCycle
}
var hasCycle = (currCourse, graph, path) => {
for (const neighbor of graph[currCourse]) {
if (isCyclic(neighbor, graph, path)) return true;
}
return false
}
/**
* https://leetcode.com/problems/course-schedule/
* Time O(V + E) | Space O(V + E)
* @param {number} numCourses
* @param {number[][]} prerequisites
* @return {boolean}
*/
var canFinish = function(numCourses, prerequisites) {
const { graph, visited, path } = buildGraph(numCourses, prerequisites);
for (let currCourse = 0; currCourse < numCourses; currCourse++) {
if (isCyclic(currCourse, graph, visited, path)) return false;
}
return true;
}
var initGraph = (numCourses) => ({
graph: new Array(numCourses).fill().map(() => []),
visited: new Array(numCourses).fill(false),
path: new Array(numCourses).fill(false)
})
var buildGraph = (numCourses, prerequisites) => {
const { graph, visited, path } = initGraph(numCourses);
for (const [ src, dst ] of prerequisites) {
const neighbors = (graph[dst] || []);
neighbors.push(src);
graph[dst] = neighbors;
}
return { graph, visited, path };
}
var isCyclic = (currCourse, graph, visited, path) => {
const isVisited = visited[currCourse]
if (isVisited) return false;
const hasSeen = path[currCourse]
if (hasSeen) return true;
const isMissingNext = !(currCourse in graph)
if (isMissingNext) return false;
const _isCyclic = backTrack(currCourse, graph, visited, path);
visited[currCourse] = true;
return _isCyclic
}
var backTrack = (currCourse, graph, visited, path) => {
path[currCourse] = true;
const _hasCycle = hasCycle(currCourse, graph, visited, path)
path[currCourse] = false;
return _hasCycle
}
var hasCycle = (currCourse, graph, visited, path) => {
for (const neighbor of graph[currCourse]) {
if (isCyclic(neighbor, graph, visited, path)) return true;
}
return false
}
/**
* https://leetcode.com/problems/course-schedule/
* Time O(V + E) | Space O(V + E)
* @param {number} numCourses
* @param {number[][]} prerequisites
* @return {boolean}
*/
var canFinish = function(numCourses, prerequisites) {
const { graph, indegree } = buildGraph(numCourses, prerequisites);
const topologicalOrder = topologicalSort(graph, indegree);
const isDirectedAcyclicGraph = topologicalOrder.length === numCourses;
return isDirectedAcyclicGraph;
};
var initGraph = (numCourses) => ({
graph: new Array(numCourses).fill().map(() => []),
indegree: new Array(numCourses).fill(0)
})
var buildGraph = (numCourses, prerequisites) => {
const { graph, indegree } = initGraph(numCourses);
for (const [ src, dst ] of prerequisites){
graph[src].push(dst);
indegree[dst]++;
}
return { graph, indegree };
}
var topologicalSort = (graph, indegree, order = []) => {
const queue = searchGraph(graph, indegree);
bfs(graph, indegree, queue, order);
return order;
}
var searchGraph = (graph, indegree, queue = new Queue([])) => {
for (const node in graph) {
const isSource = indegree[node] === 0;
if (isSource) queue.enqueue(node);
}
return queue;
}
var bfs = (graph, indegree, queue, order) => {
while (!queue.isEmpty()) {
for (let i = (queue.size() - 1); 0 <= i; i--) {
checkNeighbors(graph, indegree, queue, order);
}
}
}
var checkNeighbors = (graph, indegree, queue, order) => {
const node = queue.dequeue();
order.push(node);
for (const neighbor of graph[node]) {
indegree[neighbor]--;
const isSource = indegree[neighbor] === 0;
if (isSource) queue.enqueue(neighbor);
}
}