-
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 2100: Find Good Days to Rob the Bank
- Loading branch information
Showing
3 changed files
with
106 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
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,24 @@ | ||
pub mod sliding_window; | ||
|
||
pub trait Solution { | ||
fn good_days_to_rob_bank(security: Vec<i32>, time: i32) -> Vec<i32>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((&[5, 3, 3, 3, 5, 6, 2] as &[_], 2), &[2, 3] as &[_]), | ||
((&[1, 1, 1, 1, 1], 0), &[0, 1, 2, 3, 4]), | ||
((&[1, 2, 3, 4, 5, 6], 2), &[]), | ||
((&[1, 2, 5, 4, 1, 0, 2, 4, 5, 3, 1, 2, 4, 3, 2, 4, 8], 2), &[5, 10, 14]), | ||
((&[1], 5), &[]), | ||
]; | ||
|
||
for ((security, time), expected) in test_cases { | ||
assert_eq!(S::good_days_to_rob_bank(security.to_vec(), time), expected); | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/problem_2100_find_good_days_to_rob_the_bank/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,81 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn good_days_to_rob_bank(security: Vec<i32>, time: i32) -> Vec<i32> { | ||
let security = security.into_iter().map(|x| x as u32).collect::<Vec<_>>(); | ||
let time = time as u32 as usize; | ||
let mut result = Vec::new(); | ||
|
||
if security.len() > time * 2 { | ||
let (first_chunk, rest) = security.split_at(time); | ||
let (second_chunk, third_chunk) = rest.split_at(time); | ||
let mut left_length = 0; | ||
let mut left_prev = 0; | ||
|
||
for &value in first_chunk.iter().rev() { | ||
if value < left_prev { | ||
break; | ||
} | ||
|
||
left_length += 1; | ||
left_prev = value; | ||
} | ||
|
||
let mut right_length = 0; | ||
let mut right_prev = u32::MAX; | ||
|
||
for &value in second_chunk.iter().rev() { | ||
if value > right_prev { | ||
break; | ||
} | ||
|
||
right_length += 1; | ||
right_prev = value; | ||
} | ||
|
||
right_prev = security.get((time * 2).wrapping_sub(1)).copied().unwrap_or(u32::MAX); | ||
|
||
for (day, (&left, &right)) in (time as u32..).zip(rest.iter().zip(third_chunk)) { | ||
if left_prev < left { | ||
left_length = 1; | ||
} else { | ||
left_length += 1; | ||
} | ||
|
||
left_prev = left; | ||
|
||
if right < right_prev { | ||
right_length = 1; | ||
} else { | ||
right_length += 1; | ||
} | ||
|
||
right_prev = right; | ||
|
||
if left_length > time as u32 && right_length > time as u32 { | ||
result.push(day as _); | ||
} | ||
} | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn good_days_to_rob_bank(security: Vec<i32>, time: i32) -> Vec<i32> { | ||
Self::good_days_to_rob_bank(security, time) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |