20240328 Finished
This commit is contained in:
parent
c2456ffe48
commit
402a023105
|
@ -87,3 +87,4 @@ mod p322_coin_change;
|
||||||
mod p518_coin_change_ii;
|
mod p518_coin_change_ii;
|
||||||
mod p2642_design_graph_with_shortest_path_calculator;
|
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;
|
|
@ -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() {
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user