|
| 1 | +/** |
| 2 | + * [207] Course Schedule |
| 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, is it possible for you to finish all courses? |
| 9 | + * |
| 10 | + * Example 1: |
| 11 | + * |
| 12 | + * |
| 13 | + * Input: 2, [[1,0]] |
| 14 | + * Output: true |
| 15 | + * Explanation: There are a total of 2 courses to take. |
| 16 | + * To take course 1 you should have finished course 0. So it is possible. |
| 17 | + * |
| 18 | + * Example 2: |
| 19 | + * |
| 20 | + * |
| 21 | + * Input: 2, [[1,0],[0,1]] |
| 22 | + * Output: false |
| 23 | + * Explanation: There are a total of 2 courses to take. |
| 24 | + * To take course 1 you should have finished course 0, and to take course 0 you should |
| 25 | + * also have finished course 1. So it is impossible. |
| 26 | + * |
| 27 | + * |
| 28 | + * Note: |
| 29 | + * |
| 30 | + * <ol> |
| 31 | + * 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>. |
| 32 | + * You may assume that there are no duplicate edges in the input prerequisites. |
| 33 | + * </ol> |
| 34 | + * |
| 35 | + */ |
| 36 | +pub struct Solution {} |
| 37 | + |
| 38 | +// submission codes start here |
| 39 | + |
| 40 | +// topology sort, BFS |
| 41 | +use std::collections::VecDeque; |
| 42 | +impl Solution { |
| 43 | + pub fn can_finish(num_courses: i32, prerequisites: Vec<Vec<i32>>) -> bool { |
| 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 cnt = 0; |
| 62 | + while let Some(node) = deq.pop_front() { |
| 63 | + cnt += 1; |
| 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 | + cnt == num_courses |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// submission codes end |
| 78 | + |
| 79 | +#[cfg(test)] |
| 80 | +mod tests { |
| 81 | + use super::*; |
| 82 | + |
| 83 | + #[test] |
| 84 | + 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); |
| 91 | + } |
| 92 | +} |
0 commit comments