-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
42 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 |
---|---|---|
|
@@ -100,3 +100,4 @@ mod square_numbers; | |
mod minimized_maximum; | ||
mod remove_k_digits; | ||
mod range_freq; | ||
mod power_of_subarrays; |
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,40 @@ | ||
pub fn results_array(numbers: Vec<i32>, k: i32) -> Vec<i32> { | ||
numbers | ||
.windows(k as usize) | ||
.map(|w| { | ||
let (mut prev, mut max) = (w[0], w[0]); | ||
for &val in w.iter().skip(1) { | ||
if prev + 1 != val { | ||
return -1; | ||
} | ||
max = max.max(val); | ||
prev = val; | ||
} | ||
max | ||
}) | ||
.collect() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::results_array; | ||
|
||
#[test] | ||
fn non_consecutive_values() { | ||
assert_eq!(vec![-1, -1], results_array(vec![2, 2, 2, 2, 2], 4)) | ||
} | ||
|
||
#[test] | ||
fn zipped_consecutive_values() { | ||
assert_eq!( | ||
vec![-1, 3, -1, 3, -1], | ||
results_array(vec![3, 2, 3, 2, 3, 2], 2) | ||
) | ||
} | ||
|
||
#[test] | ||
fn k_equals_n() { | ||
assert_eq!(vec![3], results_array(vec![2, 3], 2)); | ||
assert_eq!(vec![-1], results_array(vec![3, 2], 2)) | ||
} | ||
} |