Skip to content

Commit b0c2b20

Browse files
committed
Solve #210
1 parent c80885b commit b0c2b20

File tree

3 files changed

+120
-21
lines changed

3 files changed

+120
-21
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,4 @@ mod n0206_reverse_linked_list;
177177
mod n0207_course_schedule;
178178
mod n0208_implement_trie_prefix_tree;
179179
mod n0209_minimum_size_subarray_sum;
180+
mod n0210_course_schedule_ii;

src/n0207_course_schedule.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,36 @@
22
* [207] Course Schedule
33
*
44
* There are a total of n courses you have to take, labeled from 0 to n-1.
5-
*
5+
*
66
* Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
7-
*
7+
*
88
* Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
9-
*
9+
*
1010
* Example 1:
11-
*
12-
*
13-
* Input: 2, [[1,0]]
11+
*
12+
*
13+
* Input: 2, [[1,0]]
1414
* Output: true
15-
* Explanation: There are a total of 2 courses to take.
15+
* Explanation: There are a total of 2 courses to take.
1616
* To take course 1 you should have finished course 0. So it is possible.
17-
*
17+
*
1818
* Example 2:
19-
*
20-
*
19+
*
20+
*
2121
* Input: 2, [[1,0],[0,1]]
2222
* Output: false
23-
* Explanation: There are a total of 2 courses to take.
23+
* Explanation: There are a total of 2 courses to take.
2424
* To take course 1 you should have finished course 0, and to take course 0 you should
2525
* also have finished course 1. So it is impossible.
26-
*
27-
*
26+
*
27+
*
2828
* Note:
29-
*
29+
*
3030
* <ol>
3131
* The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about <a href="https://www.khanacademy.org/computing/computer-science/algorithms/graph-representation/a/representing-graphs" target="_blank">how a graph is represented</a>.
3232
* You may assume that there are no duplicate edges in the input prerequisites.
3333
* </ol>
34-
*
34+
*
3535
*/
3636
pub struct Solution {}
3737

@@ -82,11 +82,21 @@ mod tests {
8282

8383
#[test]
8484
fn test_207() {
85-
assert_eq!(Solution::can_finish(2, vec![vec![1,0]]), true);
86-
assert_eq!(Solution::can_finish(2, vec![vec![1,0],vec![0,1]]), false);
87-
assert_eq!(Solution::can_finish(
88-
8,
89-
vec![vec![1,0],vec![2,6],vec![1,7],vec![6,4],vec![7,0],vec![0,5]]),
90-
true);
85+
assert_eq!(Solution::can_finish(2, vec![vec![1, 0]]), true);
86+
assert_eq!(Solution::can_finish(2, vec![vec![1, 0], vec![0, 1]]), false);
87+
assert_eq!(
88+
Solution::can_finish(
89+
8,
90+
vec![
91+
vec![1, 0],
92+
vec![2, 6],
93+
vec![1, 7],
94+
vec![6, 4],
95+
vec![7, 0],
96+
vec![0, 5]
97+
]
98+
),
99+
true
100+
);
91101
}
92102
}

src/n0210_course_schedule_ii.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* [210] Course Schedule II
3+
*
4+
* There are a total of n courses you have to take, labeled from 0 to n-1.
5+
*
6+
* Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
7+
*
8+
* Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.
9+
*
10+
* There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.
11+
*
12+
* Example 1:
13+
*
14+
*
15+
* Input: 2, [[1,0]]
16+
* Output: [0,1]
17+
* Explanation: There are a total of 2 courses to take. To take course 1 you should have finished
18+
* course 0. So the correct course order is [0,1] .
19+
*
20+
* Example 2:
21+
*
22+
*
23+
* Input: 4, [[1,0],[2,0],[3,1],[3,2]]
24+
* Output: [0,1,2,3] or [0,2,1,3]
25+
* Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both
26+
* courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
27+
* So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3] .
28+
*
29+
* Note:
30+
*
31+
* <ol>
32+
* The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about <a href="https://www.khanacademy.org/computing/computer-science/algorithms/graph-representation/a/representing-graphs" target="_blank">how a graph is represented</a>.
33+
* You may assume that there are no duplicate edges in the input prerequisites.
34+
* </ol>
35+
*
36+
*/
37+
pub struct Solution {}
38+
39+
// submission codes start here
40+
41+
use std::collections::VecDeque;
42+
impl Solution {
43+
pub fn find_order(num_courses: i32, prerequisites: Vec<Vec<i32>>) -> Vec<i32> {
44+
let num = num_courses as usize;
45+
let mut matrix = vec![vec![false; num]; num];
46+
let mut in_degree = vec![0; num];
47+
// collects node in degree
48+
for pre in prerequisites.iter() {
49+
if !matrix[pre[1] as usize][pre[0] as usize] {
50+
in_degree[pre[0] as usize] += 1;
51+
}
52+
matrix[pre[1] as usize][pre[0] as usize] = true;
53+
}
54+
let mut deq = VecDeque::new();
55+
// BFS starts with nodes with 0 in degree
56+
for (node, &v) in in_degree.iter().enumerate() {
57+
if v == 0 {
58+
deq.push_back(node);
59+
}
60+
}
61+
let mut res = Vec::with_capacity(num);
62+
while let Some(node) = deq.pop_front() {
63+
res.push(node as i32);
64+
for (i, &connect) in matrix[node].iter().enumerate() {
65+
if connect {
66+
in_degree[i] -= 1;
67+
if in_degree[i] == 0 {
68+
deq.push_back(i);
69+
}
70+
}
71+
}
72+
}
73+
if res.len() == num { res } else { vec![] }
74+
}
75+
}
76+
77+
// submission codes end
78+
79+
#[cfg(test)]
80+
mod tests {
81+
use super::*;
82+
83+
#[test]
84+
fn test_210() {
85+
assert_eq!(Solution::find_order(2, vec![vec![1, 0]]), vec![0, 1]);
86+
assert_eq!(Solution::find_order(4, vec![vec![1, 0], vec![2,0], vec![3,1], vec![3,2]]), vec![0,1,2,3]);
87+
}
88+
}

0 commit comments

Comments
 (0)