-
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 2256: Minimum Average Difference
- Loading branch information
Showing
3 changed files
with
74 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,55 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn minimum_average_difference(nums: Vec<i32>) -> i32 { | ||
let mut left_sum = 0; | ||
let mut left_count = 0; | ||
let mut right_sum = nums.iter().fold(0_u64, |sum, &x| sum + u64::from(x as u32)); | ||
let mut right_count = nums.len() as u64; | ||
let mut min_diff = u64::MAX; | ||
let mut min_diff_index = 0; | ||
|
||
for num in nums { | ||
let num = u64::from(num as u32); | ||
|
||
left_count += 1; | ||
right_count -= 1; | ||
left_sum += num; | ||
right_sum -= num; | ||
|
||
let left_average = left_sum / left_count; | ||
let right_average = if right_count == 0 { 0 } else { right_sum / right_count }; | ||
|
||
let diff = left_average.abs_diff(right_average); | ||
|
||
if diff < min_diff { | ||
min_diff = diff; | ||
min_diff_index = left_count; | ||
|
||
if diff == 0 { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
(min_diff_index - 1) as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn minimum_average_difference(nums: Vec<i32>) -> i32 { | ||
Self::minimum_average_difference(nums) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
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 iterative; | ||
|
||
pub trait Solution { | ||
fn minimum_average_difference(nums: Vec<i32>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [(&[2, 5, 3, 9, 5, 3] as &[_], 3), (&[0], 0)]; | ||
|
||
for (nums, expected) in test_cases { | ||
assert_eq!(S::minimum_average_difference(nums.to_vec()), expected); | ||
} | ||
} | ||
} |