-
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 2033: Minimum Operations to Make a Uni-Value Grid
- Loading branch information
Showing
3 changed files
with
81 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
57 changes: 57 additions & 0 deletions
57
src/problem_2033_minimum_operations_to_make_a_uni_value_grid/median.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,57 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::num::NonZeroU32; | ||
|
||
impl Solution { | ||
pub fn min_operations(grid: Vec<Vec<i32>>, x: i32) -> i32 { | ||
let x = NonZeroU32::new(x as _).unwrap(); | ||
let mut values = Vec::with_capacity(grid.first().map_or(0, Vec::len) * grid.len()); | ||
let remainder = grid[0][0] as u32 % x; | ||
|
||
for row in grid { | ||
for value in row { | ||
let value = value as u32; | ||
|
||
if value % x == remainder { | ||
let value = value / x; | ||
|
||
values.push(value); | ||
} else { | ||
return -1; | ||
} | ||
} | ||
} | ||
|
||
let n = values.len(); | ||
let (left, &mut middle, right) = values.select_nth_unstable(n / 2); | ||
let mut result = 0; | ||
|
||
for &mut value in left { | ||
result += middle - value; | ||
} | ||
|
||
for &mut value in right { | ||
result += value - middle; | ||
} | ||
|
||
result as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn min_operations(grid: Vec<Vec<i32>>, x: i32) -> i32 { | ||
Self::min_operations(grid, x) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/problem_2033_minimum_operations_to_make_a_uni_value_grid/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,23 @@ | ||
pub mod median; | ||
|
||
pub trait Solution { | ||
fn min_operations(grid: Vec<Vec<i32>>, x: i32) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
use crate::test_utilities::Matrix; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
((&[[2, 4], [6, 8]] as &dyn Matrix<_>, 2), 4), | ||
((&[[1, 5], [2, 3]], 1), 5), | ||
((&[[1, 2], [3, 4]], 2), -1), | ||
]; | ||
|
||
for ((grid, x), expected) in test_cases { | ||
assert_eq!(S::min_operations(grid.to_vec(), x), expected); | ||
} | ||
} | ||
} |