20240328 Finished

This commit is contained in:
jackfiled 2024-03-28 10:27:48 +08:00
parent c2456ffe48
commit 402a023105
2 changed files with 41 additions and 1 deletions

View File

@ -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;
mod p2580_count_ways_to_group_overlapping_ranges;
mod p1997_first_day_where_you_have_been_in_all_the_rooms;

View File

@ -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>) -> 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() {
}
}