You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: dsa-solutions/lc-solutions/0200-0299/0207-course-schedule.md
+11-9Lines changed: 11 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ description: "This document provides a solution to the Course Schedule problem."
12
12
---
13
13
14
14
## Problem
15
-
You are given a total of numCourses courses labeled from 0 to numCourses - 1. You are also given an array prerequisites where prerequisites[i] = [ a<sub> i</sub> , b<sub> i</sub> ] indicates that you must take course b<sub> i</sub> first if you want to take course a<sub> i</sub> .
15
+
You are given a total of numCourses courses labeled from 0 to numCourses - 1. You are also given an array prerequisites where $\text{prerequisites[i]} = [ a^i , b^i ]$ indicates that you must take course $b^i$ first if you want to take course $a^i$ .
16
16
17
17
For example, the pair [0, 1] indicates that to take course 0 you have to first take course 1.
18
18
Return `true` if you can finish all courses. Otherwise, return `false`.
@@ -36,10 +36,10 @@ To take course 1 you should have finished course 0, and to take course 0 you sho
36
36
```
37
37
38
38
### Constraints:
39
-
-```1 <= numCourses <= 2000```
40
-
-```0 <= prerequisites.length <= 5000```
41
-
-```prerequisites[i].length == 2```
42
-
-```0 <= ai, bi < numCourses ```
39
+
-`1 <= numCourses <= 2000`
40
+
-`0 <= prerequisites.length <= 5000`
41
+
- ```prerequisites[i].length == 2`
42
+
-`0 <= ai, bi < numCourses `
43
43
- All the pairs prerequisites[i] are **unique**.
44
44
## Solution
45
45
The code aims to solve the problem of determining whether it is possible to finish all the given courses without any cyclic dependencies. It uses the topological sort algorithm, specifically Kahn's algorithm, to solve this problem.
@@ -197,16 +197,18 @@ class Solution:
197
197
198
198
## Complexity Analysis
199
199
200
-
### Time Complexity: O(N + P)
200
+
### Time Complexity: $O(N + P)$
201
201
202
202
> **Reason**: Where N is the number of courses and P is the number of prerequisites.
203
203
204
-
### Space Complexity: O(N + P)
204
+
### Space Complexity: $O(N + P)$
205
205
206
-
> **Reason**: We use an adjacency list to store the graph and an array to store the in-degree of each node.
206
+
:::note
207
+
**Reason**: We use an adjacency list to store the graph and an array to store the in-degree of each node.
0 commit comments