diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /keys_and_rooms.rs | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'keys_and_rooms.rs')
-rw-r--r-- | keys_and_rooms.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/keys_and_rooms.rs b/keys_and_rooms.rs new file mode 100644 index 0000000..678c732 --- /dev/null +++ b/keys_and_rooms.rs @@ -0,0 +1,23 @@ +struct Solution; + +impl Solution { + pub fn can_visit_all_rooms(rooms: Vec<Vec<i32>>) -> bool { + let mut unlocked = vec![false; rooms.len()]; + unlocked[0] = true; + let mut stack = vec![0]; + while let Some(next) = stack.pop() { + for &i in &rooms[next as usize] { + if !unlocked[i as usize] { + unlocked[i as usize] = true; + stack.push(i); + } + } + } + unlocked.iter().all(|&b| b) + } +} + +fn main() { + let rooms = vec![vec![1], vec![2], vec![3], vec![]]; + print!("{}", Solution::can_visit_all_rooms(rooms)); +} |