Skip to content

Commit 9cd7de6

Browse files
committed
feat: add solutions to lc problem: No.0210
No.0210.Course Schedule II
1 parent 211740e commit 9cd7de6

File tree

7 files changed

+151
-128
lines changed

7 files changed

+151
-128
lines changed

solution/0200-0299/0210.Course Schedule II/README.md

Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58-
拓扑排序,BFS 实现。
58+
**方法一:拓扑排序**
59+
60+
对于本题,我们可以将课程看作图中的节点,先修课程看作图中的边,那么我们可以将本题转化为判断有向图中是否存在环。
61+
62+
具体地,我们可以使用拓扑排序的思想,对于每个入度为 $0$ 的节点,我们将其出度的节点的入度减 $1$,直到所有节点都被遍历到。
63+
64+
如果所有节点都被遍历到,说明图中不存在环,那么我们就可以完成所有课程的学习;否则,我们就无法完成所有课程的学习。
65+
66+
时间复杂度 $O(n + m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别为课程数和先修课程数。
5967

6068
<!-- tabs:start -->
6169

@@ -71,8 +79,8 @@ class Solution:
7179
for a, b in prerequisites:
7280
g[b].append(a)
7381
indeg[a] += 1
74-
q = deque([i for i, v in enumerate(indeg) if v == 0])
7582
ans = []
83+
q = deque(i for i, x in enumerate(indeg) if x == 0)
7684
while q:
7785
i = q.popleft()
7886
ans.append(i)
@@ -120,36 +128,6 @@ class Solution {
120128
}
121129
```
122130

123-
### **TypeScript**
124-
125-
```ts
126-
function findOrder(numCourses: number, prerequisites: number[][]): number[] {
127-
let g = Array.from({ length: numCourses }, () => []);
128-
let indeg = new Array(numCourses).fill(0);
129-
for (let [a, b] of prerequisites) {
130-
g[b].push(a);
131-
++indeg[a];
132-
}
133-
let q = [];
134-
for (let i = 0; i < numCourses; ++i) {
135-
if (!indeg[i]) {
136-
q.push(i);
137-
}
138-
}
139-
let ans = [];
140-
while (q.length) {
141-
const i = q.shift();
142-
ans.push(i);
143-
for (let j of g[i]) {
144-
if (--indeg[j] == 0) {
145-
q.push(j);
146-
}
147-
}
148-
}
149-
return ans.length == numCourses ? ans : [];
150-
}
151-
```
152-
153131
### **C++**
154132

155133
```cpp
@@ -164,15 +142,21 @@ public:
164142
++indeg[a];
165143
}
166144
queue<int> q;
167-
for (int i = 0; i < numCourses; ++i)
168-
if (indeg[i] == 0) q.push(i);
145+
for (int i = 0; i < numCourses; ++i) {
146+
if (indeg[i] == 0) {
147+
q.push(i);
148+
}
149+
}
169150
vector<int> ans;
170151
while (!q.empty()) {
171152
int i = q.front();
172153
q.pop();
173154
ans.push_back(i);
174-
for (int j : g[i])
175-
if (--indeg[j] == 0) q.push(j);
155+
for (int j : g[i]) {
156+
if (--indeg[j] == 0) {
157+
q.push(j);
158+
}
159+
}
176160
}
177161
return ans.size() == numCourses ? ans : vector<int>();
178162
}
@@ -191,8 +175,8 @@ func findOrder(numCourses int, prerequisites [][]int) []int {
191175
indeg[a]++
192176
}
193177
q := []int{}
194-
for i, v := range indeg {
195-
if v == 0 {
178+
for i, x := range indeg {
179+
if x == 0 {
196180
q = append(q, i)
197181
}
198182
}
@@ -215,37 +199,66 @@ func findOrder(numCourses int, prerequisites [][]int) []int {
215199
}
216200
```
217201

202+
### **TypeScript**
203+
204+
```ts
205+
function findOrder(numCourses: number, prerequisites: number[][]): number[] {
206+
const g: number[][] = new Array(numCourses).fill(0).map(() => []);
207+
const indeg: number[] = new Array(numCourses).fill(0);
208+
for (const [a, b] of prerequisites) {
209+
g[b].push(a);
210+
indeg[a]++;
211+
}
212+
const q: number[] = [];
213+
for (let i = 0; i < numCourses; ++i) {
214+
if (indeg[i] == 0) {
215+
q.push(i);
216+
}
217+
}
218+
const ans: number[] = [];
219+
while (q.length) {
220+
const i = q.shift()!;
221+
ans.push(i);
222+
for (const j of g[i]) {
223+
if (--indeg[j] == 0) {
224+
q.push(j);
225+
}
226+
}
227+
}
228+
return ans.length === numCourses ? ans : [];
229+
}
230+
```
231+
218232
### **C#**
219233

220234
```cs
221235
public class Solution {
222236
public int[] FindOrder(int numCourses, int[][] prerequisites) {
223237
var g = new List<int>[numCourses];
224-
for (int i = 0; i < numCourses; ++i)
225-
{
238+
for (int i = 0; i < numCourses; ++i) {
226239
g[i] = new List<int>();
227240
}
228241
var indeg = new int[numCourses];
229-
foreach (var p in prerequisites)
230-
{
242+
foreach (var p in prerequisites) {
231243
int a = p[0], b = p[1];
232244
g[b].Add(a);
233245
++indeg[a];
234246
}
235247
var q = new Queue<int>();
236-
for (int i = 0; i < numCourses; ++i)
237-
{
238-
if (indeg[i] == 0) q.Enqueue(i);
248+
for (int i = 0; i < numCourses; ++i) {
249+
if (indeg[i] == 0) {
250+
q.Enqueue(i);
251+
}
239252
}
240253
var ans = new int[numCourses];
241254
var cnt = 0;
242-
while (q.Count > 0)
243-
{
255+
while (q.Count > 0) {
244256
int i = q.Dequeue();
245257
ans[cnt++] = i;
246-
foreach (int j in g[i])
247-
{
248-
if (--indeg[j] == 0) q.Enqueue(j);
258+
foreach (int j in g[i]) {
259+
if (--indeg[j] == 0) {
260+
q.Enqueue(j);
261+
}
249262
}
250263
}
251264
return cnt == numCourses ? ans : new int[0];

solution/0200-0299/0210.Course Schedule II/README_EN.md

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ class Solution:
6363
for a, b in prerequisites:
6464
g[b].append(a)
6565
indeg[a] += 1
66-
q = deque([i for i, v in enumerate(indeg) if v == 0])
6766
ans = []
67+
q = deque(i for i, x in enumerate(indeg) if x == 0)
6868
while q:
6969
i = q.popleft()
7070
ans.append(i)
@@ -110,36 +110,6 @@ class Solution {
110110
}
111111
```
112112

113-
### **TypeScript**
114-
115-
```ts
116-
function findOrder(numCourses: number, prerequisites: number[][]): number[] {
117-
let g = Array.from({ length: numCourses }, () => []);
118-
let indeg = new Array(numCourses).fill(0);
119-
for (let [a, b] of prerequisites) {
120-
g[b].push(a);
121-
++indeg[a];
122-
}
123-
let q = [];
124-
for (let i = 0; i < numCourses; ++i) {
125-
if (!indeg[i]) {
126-
q.push(i);
127-
}
128-
}
129-
let ans = [];
130-
while (q.length) {
131-
const i = q.shift();
132-
ans.push(i);
133-
for (let j of g[i]) {
134-
if (--indeg[j] == 0) {
135-
q.push(j);
136-
}
137-
}
138-
}
139-
return ans.length == numCourses ? ans : [];
140-
}
141-
```
142-
143113
### **C++**
144114

145115
```cpp
@@ -154,15 +124,21 @@ public:
154124
++indeg[a];
155125
}
156126
queue<int> q;
157-
for (int i = 0; i < numCourses; ++i)
158-
if (indeg[i] == 0) q.push(i);
127+
for (int i = 0; i < numCourses; ++i) {
128+
if (indeg[i] == 0) {
129+
q.push(i);
130+
}
131+
}
159132
vector<int> ans;
160133
while (!q.empty()) {
161134
int i = q.front();
162135
q.pop();
163136
ans.push_back(i);
164-
for (int j : g[i])
165-
if (--indeg[j] == 0) q.push(j);
137+
for (int j : g[i]) {
138+
if (--indeg[j] == 0) {
139+
q.push(j);
140+
}
141+
}
166142
}
167143
return ans.size() == numCourses ? ans : vector<int>();
168144
}
@@ -181,8 +157,8 @@ func findOrder(numCourses int, prerequisites [][]int) []int {
181157
indeg[a]++
182158
}
183159
q := []int{}
184-
for i, v := range indeg {
185-
if v == 0 {
160+
for i, x := range indeg {
161+
if x == 0 {
186162
q = append(q, i)
187163
}
188164
}
@@ -205,37 +181,66 @@ func findOrder(numCourses int, prerequisites [][]int) []int {
205181
}
206182
```
207183

184+
### **TypeScript**
185+
186+
```ts
187+
function findOrder(numCourses: number, prerequisites: number[][]): number[] {
188+
const g: number[][] = new Array(numCourses).fill(0).map(() => []);
189+
const indeg: number[] = new Array(numCourses).fill(0);
190+
for (const [a, b] of prerequisites) {
191+
g[b].push(a);
192+
indeg[a]++;
193+
}
194+
const q: number[] = [];
195+
for (let i = 0; i < numCourses; ++i) {
196+
if (indeg[i] == 0) {
197+
q.push(i);
198+
}
199+
}
200+
const ans: number[] = [];
201+
while (q.length) {
202+
const i = q.shift()!;
203+
ans.push(i);
204+
for (const j of g[i]) {
205+
if (--indeg[j] == 0) {
206+
q.push(j);
207+
}
208+
}
209+
}
210+
return ans.length === numCourses ? ans : [];
211+
}
212+
```
213+
208214
### **C#**
209215

210216
```cs
211217
public class Solution {
212218
public int[] FindOrder(int numCourses, int[][] prerequisites) {
213219
var g = new List<int>[numCourses];
214-
for (int i = 0; i < numCourses; ++i)
215-
{
220+
for (int i = 0; i < numCourses; ++i) {
216221
g[i] = new List<int>();
217222
}
218223
var indeg = new int[numCourses];
219-
foreach (var p in prerequisites)
220-
{
224+
foreach (var p in prerequisites) {
221225
int a = p[0], b = p[1];
222226
g[b].Add(a);
223227
++indeg[a];
224228
}
225229
var q = new Queue<int>();
226-
for (int i = 0; i < numCourses; ++i)
227-
{
228-
if (indeg[i] == 0) q.Enqueue(i);
230+
for (int i = 0; i < numCourses; ++i) {
231+
if (indeg[i] == 0) {
232+
q.Enqueue(i);
233+
}
229234
}
230235
var ans = new int[numCourses];
231236
var cnt = 0;
232-
while (q.Count > 0)
233-
{
237+
while (q.Count > 0) {
234238
int i = q.Dequeue();
235239
ans[cnt++] = i;
236-
foreach (int j in g[i])
237-
{
238-
if (--indeg[j] == 0) q.Enqueue(j);
240+
foreach (int j in g[i]) {
241+
if (--indeg[j] == 0) {
242+
q.Enqueue(j);
243+
}
239244
}
240245
}
241246
return cnt == numCourses ? ans : new int[0];

solution/0200-0299/0210.Course Schedule II/Solution.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@ class Solution {
99
++indeg[a];
1010
}
1111
queue<int> q;
12-
for (int i = 0; i < numCourses; ++i)
13-
if (indeg[i] == 0) q.push(i);
12+
for (int i = 0; i < numCourses; ++i) {
13+
if (indeg[i] == 0) {
14+
q.push(i);
15+
}
16+
}
1417
vector<int> ans;
1518
while (!q.empty()) {
1619
int i = q.front();
1720
q.pop();
1821
ans.push_back(i);
19-
for (int j : g[i])
20-
if (--indeg[j] == 0) q.push(j);
22+
for (int j : g[i]) {
23+
if (--indeg[j] == 0) {
24+
q.push(j);
25+
}
26+
}
2127
}
2228
return ans.size() == numCourses ? ans : vector<int>();
2329
}

0 commit comments

Comments
 (0)