Skip to content

Commit

Permalink
Add problem 2327: Number of People Aware of a Secret
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Nov 26, 2024
1 parent bb7ef30 commit b7cde90
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,7 @@ pub mod problem_2320_count_number_of_ways_to_place_houses;
pub mod problem_2321_maximum_score_of_spliced_array;
pub mod problem_2325_decode_the_message;
pub mod problem_2326_spiral_matrix_iv;
pub mod problem_2327_number_of_people_aware_of_a_secret;
pub mod problem_2331_evaluate_boolean_binary_tree;
pub mod problem_2335_minimum_amount_of_time_to_fill_cups;
pub mod problem_2336_smallest_number_in_infinite_set;
Expand Down
18 changes: 18 additions & 0 deletions src/problem_2327_number_of_people_aware_of_a_secret/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub mod sliding_window;

pub trait Solution {
fn people_aware_of_secret(n: i32, delay: i32, forget: i32) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [((6, 2, 4), 5), ((4, 1, 3), 6), ((684, 18, 496), 653_668_527)];

for ((n, delay, forget), expected) in test_cases {
assert_eq!(S::people_aware_of_secret(n, delay, forget), expected);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
const MODULUS: u32 = 1_000_000_007;

fn assign_add_sub(target: &mut u32, add: u32, subtract: u32) {
*target += add;
*target += Self::MODULUS - subtract;

if *target >= Self::MODULUS {
*target -= Self::MODULUS;
}

if *target >= Self::MODULUS {
*target -= Self::MODULUS;
}
}

pub fn people_aware_of_secret(n: i32, delay: i32, forget: i32) -> i32 {
let n = n as u32 as usize;

assert!(n <= 1000);

let delay = delay as u32 as usize;
let forget = forget as u32 as usize;
let mut cache = [0_u32; 1000];

cache[0] = 1;

let mut sum = 0_u32;
let new_offset = delay;
let old_offset = forget;

for day in 1..n {
let spreading_count = cache.get(day.wrapping_sub(old_offset)).copied().unwrap_or(0);

Self::assign_add_sub(
&mut sum,
cache.get(day.wrapping_sub(new_offset)).copied().unwrap_or(0),
spreading_count,
);

cache[day] = sum;
}

cache[n - forget..n].iter().fold(0_u32, |mut sum, &x| {
sum += x;

sum.checked_sub(Self::MODULUS).unwrap_or(sum)
}) as _
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn people_aware_of_secret(n: i32, delay: i32, forget: i32) -> i32 {
Self::people_aware_of_secret(n, delay, forget)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}

0 comments on commit b7cde90

Please sign in to comment.