-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.rs
57 lines (56 loc) · 1.75 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
53
54
55
56
57
/**
* 203 / 203 test cases passed.
* Runtime: 16 ms
* Memory Usage: 2.3 MB
*/
impl Solution {
pub fn image_smoother(img: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let (m, n) = (img.len(), img[0].len());
let mut ret = vec![vec![0; n]; m];
for i in 0..m {
for j in 0..n {
let mut count = 0;
for x in [i - 1, i, i + 1] {
for y in [j - 1, j, j + 1] {
if 0 <= x && x < m && 0 <= y && y < n {
ret[i][j] += img[x][y];
count += 1;
}
}
}
ret[i][j] /= count;
}
}
ret
}
}
/**
* 203 / 203 test cases passed.
* Runtime: 16 ms
* Memory Usage: 2.3 MB
*/
use std::cmp::{max, min};
impl Solution2 {
pub fn image_smoother(img: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let (m, n) = (img.len(), img[0].len());
let mut psum = vec![vec![0; n + 1]; m + 1];
let mut ret = vec![vec![0; n]; m];
for i in 1..=m {
for j in 1..=n {
psum[i][j] = psum[i - 1][j] + psum[i][j - 1] - psum[i - 1][j - 1] + img[i - 1][j - 1];
}
}
for i in 0..m {
for j in 0..n {
let top = max(0, i as i32 - 1) as usize;
let left = max(0, j as i32 - 1) as usize;
let right = min(j + 1, n - 1);
let bottom = min(i + 1, m - 1);
let count = (bottom - top + 1) * (right - left + 1);
ret[i][j] = psum[bottom + 1][right + 1] - psum[bottom + 1][left] - psum[top][right + 1] + psum[top][left];
ret[i][j] /= count as i32;
}
}
ret
}
}