-
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 1968: Array With Elements Not Equal to Average of Neighbors
- Loading branch information
Showing
3 changed files
with
77 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
42 changes: 42 additions & 0 deletions
42
src/problem_1968_array_with_elements_not_equal_to_average_of_neighbors/iterative.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,42 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::mem; | ||
|
||
impl Solution { | ||
pub fn rearrange_array(nums: Vec<i32>) -> Vec<i32> { | ||
let mut nums = nums; | ||
let mut iter = nums.iter_mut(); | ||
let prev_1 = iter.next().unwrap(); | ||
let mut prev_2 = iter.next().unwrap(); | ||
let mut is_decreasing = prev_2 < prev_1; | ||
|
||
for value in iter { | ||
if is_decreasing == (value < prev_2) { | ||
mem::swap(prev_2, value); | ||
} | ||
|
||
prev_2 = value; | ||
is_decreasing = !is_decreasing; | ||
} | ||
|
||
nums | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn rearrange_array(nums: Vec<i32>) -> Vec<i32> { | ||
Self::rearrange_array(nums) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/problem_1968_array_with_elements_not_equal_to_average_of_neighbors/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,34 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn rearrange_array(nums: Vec<i32>) -> Vec<i32>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
use crate::test_utilities; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [&[1, 2, 3, 4, 5] as &[_], &[6, 2, 0, 9, 7]]; | ||
|
||
for nums in test_cases { | ||
let result = S::rearrange_array(nums.to_vec()); | ||
|
||
assert_eq!( | ||
test_utilities::unstable_sorted(result.iter().copied()), | ||
test_utilities::unstable_sorted(nums.iter().copied()), | ||
); | ||
|
||
let mut prev_1 = i32::MIN; | ||
let mut prev_2 = i32::MIN; | ||
|
||
for value in result { | ||
assert_ne!(prev_1 + value, prev_2 << 1); | ||
|
||
prev_1 = prev_2; | ||
prev_2 = value; | ||
} | ||
} | ||
} | ||
} |