1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00

problems(rs): add “1557. Minimum Number of Vertices to Reach All Nodes”

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2023-05-19 00:39:55 +02:00
parent 4bb6d712e2
commit 3a111bd4cf
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,19 @@
impl Solution {
pub fn find_smallest_set_of_vertices(n: i32, edges: Vec<Vec<i32>>) -> Vec<i32> {
// It should be possible to use topological ordering + BFS, but…
let mut is_reachable: Vec<bool> = vec![false; n as usize];
for dst in edges.iter().map(|edge| edge[1] as usize) {
is_reachable[dst] = true;
}
let mut result: Vec<i32> = vec![];
for (i, &reachable) in is_reachable.iter().enumerate() {
if !reachable {
result.push(i as i32);
}
}
result
}
}