-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2327: Number of People Aware of a Secret
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/problem_2327_number_of_people_aware_of_a_secret/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/problem_2327_number_of_people_aware_of_a_secret/sliding_window.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |