|
| 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