-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
69 lines (62 loc) · 1.48 KB
/
Copy pathmain.js
File metadata and controls
69 lines (62 loc) · 1.48 KB
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
/**
* @param {number} numCourses
* @param {number[][]} prerequisites
* @return {number[]}
*/
var findOrder = function(numCourses, prerequisites) {
const indegrees = new Array(numCourses).fill(0)
const links = Array.from({ length: numCourses }).map(_ => [])
// console.log(links)
prerequisites.forEach(([second, first]) => {
indegrees[second]++
links[first].push(second)
})
const q = new Queue()
const ans = []
;[...Array(numCourses).keys()].filter(i => !indegrees[i]).forEach(x => q.push(x))
while (!q.empty()) {
const top = q.pop()
ans.push(top)
links[top].forEach(x => indegrees[x]--)
links[top].filter(x => !indegrees[x]).forEach(x => q.push(x))
}
return ans.length === numCourses ? ans : []
};
class Queue {
constructor () {
this._front = this._back = { val: null, next: null }
this._length = 0
}
push (val) {
this._back.next = {
val,
next: null
}
this._back = this._back.next
this._length += 1
}
pop () {
if (this._length === 0) {
throw new Error('failed to pop: empty queue')
}
this._length -= 1
this._front = this._front.next
return this._front.val
}
top () {
if (this._length === 0) {
throw new Error('Failed to top: empty queue')
}
return this._front.next.val
}
get length () {
return this._length
}
empty () {
return this._length === 0
}
clear () {
this._front = this._back = { val: null, next: null }
this._length = 0
}
}