Skip to content

Commit c814693

Browse files
committed
solve #207
1 parent e0f976a commit c814693

File tree

3 files changed

+93
-18
lines changed

3 files changed

+93
-18
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,4 @@ mod n0203_remove_linked_list_elements;
174174
mod n0204_count_primes;
175175
mod n0205_isomorphic_strings;
176176
mod n0206_reverse_linked_list;
177+
mod n0207_course_schedule;

src/n0025_reverse_nodes_in_k_group.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,6 @@ use super::util::linked_list::{ListNode, to_list};
2929

3030
// submission codes start here
3131

32-
// Definition for singly-linked list.
33-
// #[derive(PartialEq, Eq, Debug)]
34-
// pub struct ListNode {
35-
// pub val: i32,
36-
// pub next: Option<Box<ListNode>>
37-
// }
38-
//
39-
// impl ListNode {
40-
// #[inline]
41-
// fn new(val: i32) -> Self {
42-
// ListNode {
43-
// next: None,
44-
// val
45-
// }
46-
// }
47-
// }
48-
49-
// TODO: kind of verbose, try to simplify
5032
impl Solution {
5133
pub fn reverse_k_group(head: Option<Box<ListNode>>, k: i32) -> Option<Box<ListNode>> {
5234
let mut dummy_head = Some(Box::new(ListNode{val: 0, next: head}));

src/n0207_course_schedule.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)