-
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 1975: Maximum Matrix Sum
- Loading branch information
Showing
3 changed files
with
70 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,45 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn max_matrix_sum(matrix: Vec<Vec<i32>>) -> i64 { | ||
let mut candidate_sum = 0_i64; | ||
let mut min_abs = i32::MAX; | ||
let mut has_odd_negatives = false; | ||
|
||
for row in matrix { | ||
for mut value in row { | ||
if value < 0 { | ||
value = -value; | ||
has_odd_negatives = !has_odd_negatives; | ||
} | ||
|
||
candidate_sum += i64::from(value); | ||
min_abs = min_abs.min(value); | ||
} | ||
} | ||
|
||
if has_odd_negatives { | ||
candidate_sum -= i64::from(min_abs) * 2; | ||
} | ||
|
||
candidate_sum | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn max_matrix_sum(matrix: Vec<Vec<i32>>) -> i64 { | ||
Self::max_matrix_sum(matrix) | ||
} | ||
} | ||
|
||
#[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,24 @@ | ||
pub mod greedy; | ||
|
||
pub trait Solution { | ||
fn max_matrix_sum(matrix: Vec<Vec<i32>>) -> i64; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
use crate::test_utilities::Matrix; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[[1, -1], [-1, 1]] as &dyn Matrix<_>, 4), | ||
(&[[1, 2, 3], [-1, -2, -3], [1, 2, 3]], 16), | ||
(&[[-1, 0, -1], [-2, 1, 3], [3, 2, 2]], 15), | ||
(&[[2, 9, 3], [5, 4, -4], [1, 7, 1]], 34), | ||
]; | ||
|
||
for (matrix, expected) in test_cases { | ||
assert_eq!(S::max_matrix_sum(matrix.to_vec()), expected); | ||
} | ||
} | ||
} |