Skip to content

Commit 2976f73

Browse files
committed
Add problem 3033: Modify the Matrix
1 parent daeb6bf commit 2976f73

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,6 +2054,7 @@ pub mod problem_3021_alice_and_bob_playing_flower_game;
20542054
pub mod problem_3024_type_of_triangle;
20552055
pub mod problem_3026_maximum_good_subarray_sum;
20562056
pub mod problem_3028_ant_on_the_boundary;
2057+
pub mod problem_3033_modify_the_matrix;
20572058

20582059
#[cfg(test)]
20592060
mod test_utilities;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn modified_matrix(matrix: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
7+
let mut matrix = matrix;
8+
let columns = matrix.first().map_or(0, Vec::len);
9+
10+
(0..columns).for_each(|column| {
11+
let max = matrix.iter().fold(i32::MIN, |max, row| max.max(row[column]));
12+
13+
for row in &mut matrix {
14+
let value = &mut row[column];
15+
16+
if *value == -1 {
17+
*value = max;
18+
}
19+
}
20+
});
21+
22+
matrix
23+
}
24+
}
25+
26+
// ------------------------------------------------------ snip ------------------------------------------------------ //
27+
28+
impl super::Solution for Solution {
29+
fn modified_matrix(matrix: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
30+
Self::modified_matrix(matrix)
31+
}
32+
}
33+
34+
#[cfg(test)]
35+
mod tests {
36+
#[test]
37+
fn test_solution() {
38+
super::super::tests::run::<super::Solution>();
39+
}
40+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn modified_matrix(matrix: Vec<Vec<i32>>) -> Vec<Vec<i32>>;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
use crate::test_utilities::Matrix;
11+
12+
pub fn run<S: Solution>() {
13+
let test_cases = [
14+
(
15+
&[[1, 2, -1], [4, -1, 6], [7, 8, 9]] as &dyn Matrix<_>,
16+
&[[1, 2, 9], [4, 8, 6], [7, 8, 9]] as &dyn Matrix<_>,
17+
),
18+
(&[[3, -1], [5, 2]], &[[3, 2], [5, 2]]),
19+
];
20+
21+
for (matrix, expected) in test_cases {
22+
assert_eq!(S::modified_matrix(matrix.to_vec()), expected);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)