-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.rs
52 lines (47 loc) · 1.35 KB
/
Solution.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
pub struct Solution {}
impl Solution {
pub fn largest_local(grid: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let n: usize = grid.len();
let m: usize = n - 3 + 1;
let mut max_local: Vec<Vec<i32>> = Vec::with_capacity(m);
for _ in 0..m {
max_local.push(vec![0; m])
}
for i in 1..n - 1 {
for j in 1..n - 1 {
max_local[i - 1][j - 1] = *grid[i - 1..=i + 1]
.iter()
.flat_map(|row| &row[j - 1..=j + 1])
.max()
.unwrap(); // safe to unwrap assuming grid has correct dimensions
}
}
max_local
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_largest_local() {
assert_eq!(
vec![vec![9, 9], vec![8, 6]],
Solution::largest_local(vec![
vec![9, 9, 8, 1],
vec![5, 6, 2, 6],
vec![8, 2, 6, 4],
vec![6, 2, 2, 2]
])
);
assert_eq!(
vec![vec![2, 2, 2], vec![2, 2, 2], vec![2, 2, 2]],
Solution::largest_local(vec![
vec![1, 1, 1, 1, 1],
vec![1, 1, 1, 1, 1],
vec![1, 1, 2, 1, 1],
vec![1, 1, 1, 1, 1],
vec![1, 1, 1, 1, 1]
])
);
}
}