Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ea6a6b0

Browse files
feat: add solutions to lc problem: No.0207 (doocs#3915)
No.0207.Course Schedule
1 parent 44960a4 commit ea6a6b0

File tree

9 files changed

+125
-184
lines changed

9 files changed

+125
-184
lines changed

‎solution/0200-0299/0207.Course Schedule/README.md‎

Lines changed: 39 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,19 @@ tags:
8080
```python
8181
class Solution:
8282
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
83-
g = defaultdict(list)
83+
g = [[] for _ inrange(numCourses)]
8484
indeg = [0] * numCourses
8585
for a, b in prerequisites:
8686
g[b].append(a)
8787
indeg[a] += 1
88-
cnt = 0
89-
q = deque(i for i, x in enumerate(indeg) if x == 0)
90-
while q:
91-
i = q.popleft()
92-
cnt += 1
88+
q = [i for i, x in enumerate(indeg) if x == 0]
89+
for i in q:
90+
numCourses -= 1
9391
for j in g[i]:
9492
indeg[j] -= 1
9593
if indeg[j] == 0:
9694
q.append(j)
97-
return cnt == numCourses
95+
return numCourses == 0
9896
```
9997

10098
#### Java
@@ -116,17 +114,16 @@ class Solution {
116114
q.offer(i);
117115
}
118116
}
119-
int cnt = 0;
120117
while (!q.isEmpty()) {
121118
int i = q.poll();
122-
++cnt;
119+
--numCourses;
123120
for (int j : g[i]) {
124121
if (--indeg[j] == 0) {
125122
q.offer(j);
126123
}
127124
}
128125
}
129-
return cnt == numCourses;
126+
return numCourses == 0;
130127
}
131128
}
132129
```
@@ -150,18 +147,17 @@ public:
150147
q.push(i);
151148
}
152149
}
153-
int cnt = 0;
154150
while (!q.empty()) {
155151
int i = q.front();
156152
q.pop();
157-
++cnt;
153+
--numCourses;
158154
for (int j : g[i]) {
159155
if (--indeg[j] == 0) {
160156
q.push(j);
161157
}
162158
}
163159
}
164-
return cnt == numCourses;
160+
return numCourses == 0;
165161
}
166162
};
167163
```
@@ -183,49 +179,46 @@ func canFinish(numCourses int, prerequisites [][]int) bool {
183179
q = append(q, i)
184180
}
185181
}
186-
cnt := 0
187182
for len(q) > 0 {
188183
i := q[0]
189184
q = q[1:]
190-
cnt++
185+
numCourses--
191186
for _, j := range g[i] {
192187
indeg[j]--
193188
if indeg[j] == 0 {
194189
q = append(q, j)
195190
}
196191
}
197192
}
198-
return cnt == numCourses
193+
return numCourses == 0
199194
}
200195
```
201196

202197
#### TypeScript
203198

204199
```ts
205200
function canFinish(numCourses: number, prerequisites: number[][]): boolean {
206-
const g: number[][] = newArray(numCourses).fill(0).map(() => []);
207-
const indeg: number[] = newArray(numCourses).fill(0);
201+
const g: number[][] = Array.from({ length: numCourses }, () => []);
202+
const indeg: number[] = Array(numCourses).fill(0);
208203
for (const [a, b] of prerequisites) {
209204
g[b].push(a);
210205
indeg[a]++;
211206
}
212207
const q: number[] = [];
213208
for (let i = 0; i < numCourses; ++i) {
214-
if (indeg[i] == 0) {
209+
if (indeg[i] === 0) {
215210
q.push(i);
216211
}
217212
}
218-
let cnt = 0;
219-
while (q.length) {
220-
const i = q.shift()!;
221-
cnt++;
213+
for (const i of q) {
214+
--numCourses;
222215
for (const j of g[i]) {
223-
if (--indeg[j] == 0) {
216+
if (--indeg[j] === 0) {
224217
q.push(j);
225218
}
226219
}
227220
}
228-
return cnt ==numCourses;
221+
return numCourses ===0;
229222
}
230223
```
231224

@@ -235,48 +228,36 @@ function canFinish(numCourses: number, prerequisites: number[][]): boolean {
235228
use std::collections::VecDeque;
236229

237230
impl Solution {
238-
#[allow(dead_code)]
239-
pub fn can_finish(num_course: i32, prerequisites: Vec<Vec<i32>>) -> bool {
240-
let num_course = num_course as usize;
241-
// The graph representation
242-
let mut graph: Vec<Vec<i32>> = vec![vec![]; num_course];
243-
// Record the in degree for each node
244-
let mut in_degree_vec: Vec<i32> = vec![0; num_course];
245-
let mut q: VecDeque<usize> = VecDeque::new();
246-
let mut count = 0;
247-
248-
// Initialize the graph & in degree vector
249-
for p in &prerequisites {
250-
let (from, to) = (p[0], p[1]);
251-
graph[from as usize].push(to);
252-
in_degree_vec[to as usize] += 1;
231+
pub fn can_finish(mut num_courses: i32, prerequisites: Vec<Vec<i32>>) -> bool {
232+
let mut g: Vec<Vec<i32>> = vec![vec![]; num_courses as usize];
233+
let mut indeg: Vec<i32> = vec![0; num_courses as usize];
234+
235+
for p in prerequisites {
236+
let a = p[0] as usize;
237+
let b = p[1] as usize;
238+
g[b].push(a as i32);
239+
indeg[a] += 1;
253240
}
254241

255-
// Enqueue the first batch of nodes with in degree 0
256-
for i in 0..num_course {
257-
if in_degree_vec[i] == 0 {
258-
q.push_back(i);
242+
letmutq:VecDeque<usize> =VecDeque::new();
243+
for i in 0..num_courses {
244+
if indeg[iasusize] == 0 {
245+
q.push_back(iasusize);
259246
}
260247
}
261248

262-
// Begin the traverse & update through the graph
263-
while !q.is_empty() {
264-
// Get the current node index
265-
let index = q.front().unwrap().clone();
266-
// This course can be finished
267-
count += 1;
268-
q.pop_front();
269-
for i in &graph[index] {
270-
// Update the in degree for the current node
271-
in_degree_vec[*i as usize] -= 1;
272-
// See if can be enqueued
273-
if in_degree_vec[*i as usize] == 0 {
274-
q.push_back(*i as usize);
249+
while let Some(i) = q.pop_front() {
250+
num_courses -= 1;
251+
for &j in &g[i] {
252+
let j = j as usize;
253+
indeg[j] -= 1;
254+
if indeg[j] == 0 {
255+
q.push_back(j);
275256
}
276257
}
277258
}
278259

279-
count == num_course
260+
num_courses == 0
280261
}
281262
}
282263
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /