Skip to content

Commit 1a74528

Browse files
committed
Add problem 2373: Largest Local Values in a Matrix
1 parent 9e6a7b4 commit 1a74528

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,7 @@ pub mod problem_2367_number_of_arithmetic_triplets;
17651765
pub mod problem_2368_reachable_nodes_with_restrictions;
17661766
pub mod problem_2369_check_if_there_is_a_valid_partition_for_the_array;
17671767
pub mod problem_2370_longest_ideal_subsequence;
1768+
pub mod problem_2373_largest_local_values_in_a_matrix;
17681769

17691770
#[cfg(test)]
17701771
mod test_utilities;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
#[expect(single_use_lifetimes, reason = "false-positive")]
7+
fn conv_1d<'a>(mut iter: impl Iterator<Item = &'a mut i32>) {
8+
let mut prev_1 = iter.next().unwrap();
9+
let mut prev_2 = iter.next().unwrap();
10+
11+
for value in iter {
12+
*prev_1 = (*prev_1).max(*prev_2).max(*value);
13+
prev_1 = prev_2;
14+
prev_2 = value;
15+
}
16+
}
17+
18+
pub fn largest_local(grid: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
19+
let mut grid = grid;
20+
let result_rows = grid.len() - 2;
21+
let result_columns = grid.first().map_or(0, Vec::len) - 2;
22+
23+
for row in &mut grid {
24+
Self::conv_1d(row.iter_mut());
25+
26+
row.truncate(result_columns);
27+
}
28+
29+
for column in 0..result_columns {
30+
Self::conv_1d(grid.iter_mut().map(|row| &mut row[column]));
31+
}
32+
33+
grid.truncate(result_rows);
34+
35+
grid
36+
}
37+
}
38+
39+
// ------------------------------------------------------ snip ------------------------------------------------------ //
40+
41+
impl super::Solution for Solution {
42+
fn largest_local(grid: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
43+
Self::largest_local(grid)
44+
}
45+
}
46+
47+
#[cfg(test)]
48+
mod tests {
49+
#[test]
50+
fn test_solution() {
51+
super::super::tests::run::<super::Solution>();
52+
}
53+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn largest_local(grid: 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+
&[[9, 9, 8, 1], [5, 6, 2, 6], [8, 2, 6, 4], [6, 2, 2, 2]] as &dyn Matrix<_>,
16+
&[[9, 9], [8, 6]] as &dyn Matrix<_>,
17+
),
18+
(
19+
&[
20+
[1, 1, 1, 1, 1],
21+
[1, 1, 1, 1, 1],
22+
[1, 1, 2, 1, 1],
23+
[1, 1, 1, 1, 1],
24+
[1, 1, 1, 1, 1],
25+
],
26+
&[[2, 2, 2], [2, 2, 2], [2, 2, 2]],
27+
),
28+
];
29+
30+
for (grid, expected) in test_cases {
31+
assert_eq!(S::largest_local(grid.to_vec()), expected);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)