forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCourseSchedule.swift
66 lines (59 loc) · 2.44 KB
/
CourseSchedule.swift
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
/**
* Question Link: https://leetcode.com/problems/course-schedule/
* Primary idea: Kahn's Algorithms
* 1) Create the graph
* 2) Decorate each vertex with its in-degree
* 3) Create a set of all sources
* 4) While the set isn’t empty,
* i. Remove a vertex from the set and add it to the sorted list
* ii. For every edge from that vertex:
* - Decrement the in-degree of the destination node
* - Check all of its destination vertices and add them to the set if they have no incoming edges
* Time Complexity: O(|E| + |V|), Space Complexity: O(n^2)
* Recommand Reading: http://cs.brown.edu/courses/csci0160/lectures/14.pdf
*/
class CourseSchedule {
func canFinish(_ numCourses: Int, _ prerequisites: [[Int]]) -> Bool {
// ATTENTION: if graph use [[Int]], will get 'memory limited exceed'
var graph = [[UInt8]](repeatElement([UInt8](repeatElement(0, count: numCourses)), count: numCourses))
var indegree = [Int](repeatElement(0, count: numCourses))
// 1. Create the graph
for i in 0..<prerequisites.count {
let course = prerequisites[i][0]
let pre = prerequisites[i][1]
// 2. Decorate each vertex with its in-degree
// Eliminate duplicate case
if graph[pre][course] == 0 {
indegree[course] += 1
}
graph[pre][course] = 1
}
// 3. Create a array of sources
var sources = [Int]()
for i in 0..<numCourses {
if indegree[i] == 0 {
sources.append(i)
}
}
//var topoSortedList = [Int]()
var count = 0
while !sources.isEmpty {
// 4.i. Remove a vertex from the set and add it to the sorted list
let source = sources.popLast()
//topoSortedList.append(source!)
count += 1
// 4.ii. Decrement the in-degree of the destination node
for i in 0..<numCourses {
if graph[source!][i] == 1 {
indegree[i] -= 1
// Check all of its destination vertices and add them to the set if they have no incoming edges
if indegree[i] == 0 {
sources.append(i)
}
}
}
}
//return topoSortedList.count == numCourses
return count == numCourses
}
}