diff --git a/src/problem/mod.rs b/src/problem/mod.rs index a7fe419..365c9e3 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -86,4 +86,5 @@ mod p2549_count_distinct_numbers_on_board; mod p322_coin_change; mod p518_coin_change_ii; mod p2642_design_graph_with_shortest_path_calculator; -mod p2580_count_ways_to_group_overlapping_ranges; \ No newline at end of file +mod p2580_count_ways_to_group_overlapping_ranges; +mod p1997_first_day_where_you_have_been_in_all_the_rooms; \ No newline at end of file diff --git a/src/problem/p1997_first_day_where_you_have_been_in_all_the_rooms.rs b/src/problem/p1997_first_day_where_you_have_been_in_all_the_rooms.rs new file mode 100644 index 0000000..6aec029 --- /dev/null +++ b/src/problem/p1997_first_day_where_you_have_been_in_all_the_rooms.rs @@ -0,0 +1,39 @@ +/** + * [1997] First Day Where You Have Been in All the Rooms + */ +pub struct Solution {} + + +// submission codes start here + +impl Solution { + pub fn first_day_been_in_all_rooms(next_visit: Vec) -> i32 { + let m = 1_000_000_007; + let mut dp = vec![0;next_visit.len()]; + dp[0] = 2; + + for i in 1..next_visit.len() { + let next = next_visit[i] as usize; + + dp[i] = dp[i - 1] + 2; + + if next != 0 { + dp[i] = (dp[i] - dp[next - 1] + m) % m; + } + dp[i] = (dp[i] + dp[i - 1]) % m; + } + + dp[next_visit.len() - 2] + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_1997() { + } +}