-
Notifications
You must be signed in to change notification settings - Fork 889
/
CourseSchedule.swift
35 lines (28 loc) · 1.08 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
/**
* Question Link: https://leetcode.com/problems/course-schedule/
* Primary idea: Topological sort, use indegree of a graph and BFS to solve the problem
*
* Time Complexity: O(n), Space Complexity: O(n)
*
*/
class CourseSchedule {
func canFinish(_ numCourses: Int, _ prerequisites: [[Int]]) -> Bool {
var inDegrees = Array(repeating: 0, count: numCourses), toCourses = [Int: [Int]]()
for courses in prerequisites {
inDegrees[courses[0]] += 1
toCourses[courses[1], default:[]].append(courses[0])
}
var queue = (0..<numCourses).filter { inDegrees[$0] == 0 }, validCoursesCount = 0
while !queue.isEmpty {
let course = queue.removeFirst()
validCoursesCount += 1
for toCourse in toCourses[course] ?? [] {
inDegrees[toCourse] -= 1
if inDegrees[toCourse] == 0 {
queue.append(toCourse)
}
}
}
return validCoursesCount == numCourses
}
}