Skip to content

Commit 34127b4

Browse files
committed
[Graph] Add a solution to Course Schedule
1 parent b0cfeee commit 34127b4

5 files changed

+55
-8
lines changed

Diff for: Graph/CourseSchedule.swift

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/course-schedule/
3+
* Primary idea: Topological sort, use indegree of a graph and BFS to solve the problem
4+
*
5+
* Time Complexity: O(n), Space Complexity: O(n)
6+
*
7+
*/
8+
9+
class CourseSchedule {
10+
func canFinish(_ numCourses: Int, _ prerequisites: [[Int]]) -> Bool {
11+
var inDegrees = Array(repeating: 0, count: numCourses), fromTo = [Int: [Int]]()
12+
var coursesCouldTake = [Int](), queue = [Int]()
13+
14+
// init graph
15+
for prerequisite in prerequisites {
16+
fromTo[prerequisite[0], default: []].append(prerequisite[1])
17+
inDegrees[prerequisite[1]] += 1
18+
}
19+
20+
// BFS
21+
for course in 0..<numCourses {
22+
if inDegrees[course] == 0 {
23+
queue.append(course)
24+
}
25+
}
26+
27+
while !queue.isEmpty {
28+
let currentCourse = queue.removeFirst()
29+
coursesCouldTake.append(currentCourse)
30+
31+
guard let toCourses = fromTo[currentCourse] else {
32+
continue
33+
}
34+
35+
toCourses.forEach {
36+
inDegrees[$0] -= 1
37+
38+
if inDegrees[$0] == 0 {
39+
queue.append($0)
40+
}
41+
}
42+
}
43+
44+
return coursesCouldTake.count == numCourses
45+
}
46+
}
File renamed without changes.
File renamed without changes.

Diff for: README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Leetcode](./logo.png?style=centerme)
55

66
## Progress
7-
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 295 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
7+
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 296 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
88

99
## Contributors
1010

@@ -25,7 +25,7 @@
2525
* [Math](#math)
2626
* [Search](#search)
2727
* [Sort](#sort)
28-
* [Union Find](#union-find)
28+
* [Graph](#graph)
2929
* [Design](#design)
3030

3131
## Companies
@@ -365,12 +365,13 @@
365365
[Insert Interval](https://leetcode.com/problems/insert-interval/description/)| [Swift](./Sort/InsertInterval.swift)|Hard| O(n)| O(1)|
366366
[Largest Number](https://leetcode.com/problems/largest-number/)| [Swift](./Sort/LargestNumber.swift)| Medium| O(nlogn)| O(1)|
367367

368-
## Union Find
368+
## Graph
369369
| Title | Solution | Difficulty | Time | Space |
370370
| ----- | -------- | ---------- | ---- | ----- |
371-
[Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/)| [Swift](./UnionFind/NumberConnectedComponentsUndirectedGraph.swift)| Medium| O(nlogn)| O(n)|
372-
[Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree/)| [Swift](./UnionFind/GraphValidTree.swift)| Medium| O(nlogn)| O(n)|
373-
[Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/)| [Swift](./UnionFind/NumberIslandsII.swift)| Hard| O(klogmn)| O(mn)|
371+
[Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/)| [Swift](./Graph/NumberConnectedComponentsUndirectedGraph.swift)| Medium| O(nlogn)| O(n)|
372+
[Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree/)| [Swift](./Graph/GraphValidTree.swift)| Medium| O(nlogn)| O(n)|
373+
[Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/)| [Swift](./Graph/NumberIslandsII.swift)| Hard| O(klogmn)| O(mn)|
374+
[Course Schedule](https://leetcode.com/problems/course-schedule/)| [Swift](./Graph/CourseSchedule.swift)| Medium| O(n)| O(n)|
374375

375376
## Design
376377
| Title | Solution | Difficulty | Time | Space |
@@ -586,7 +587,7 @@
586587
| | 308 | [Range Sum Query 2D - Mutable](https://leetcode.com/problems/range-sum-query-2d-mutable/) &hearts; | Hard
587588
| | 307 | [Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | Medium
588589
| | 306 | [Additive Number](https://leetcode.com/problems/additive-number/) | Medium
589-
| [Swift](./UnionFind/NumberIslandsII.swift) | 305 | [Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/) &hearts; | Hard
590+
| [Swift](./Graph/NumberIslandsII.swift) | 305 | [Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/) &hearts; | Hard
590591
| [Swift](./Array/NumMatrix.swift) | 304 | [Range Sum Query 2D - Immutable](https://leetcode.com/problems/range-sum-query-2d-immutable/) | Medium
591592
| | 303 | [Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable/) | Easy
592593
| | 302 | [Smallest Rectangle Enclosing Black Pixels](https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/) &hearts; | Hard
@@ -683,7 +684,7 @@
683684
| | 210 | [Course Schedule II](https://leetcode.com/problems/course-schedule-ii/) | Medium |
684685
| [Swift](./Array/MinimumSizeSubarraySum.swift) | 209 | [Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/) | Medium |
685686
| [Swift](./Design/ImplementTrie.swift) | 208 | [Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/) | Medium |
686-
| | 207 | [Course Schedule](https://leetcode.com/problems/course-schedule/) | Medium |
687+
| [Swift](./Graph/CourseSchedule.swift) | 207 | [Course Schedule](https://leetcode.com/problems/course-schedule/) | Medium |
687688
| [Swift](./LinkedList/ReverseLinkedList.swift) | 206 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | Easy |
688689
| [Swift](./String/IsomorphicStrings.swift) | 205 | [Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/) | Easy |
689690
| [Swift](./Math/CountPrimes.swift) | 204 | [Count Primes](https://leetcode.com/problems/count-primes/) | Easy |

0 commit comments

Comments
 (0)