rustgym/leetcode/
_302_smallest_rectangle_enclosing_black_pixels.rs

1struct Solution;
2
3impl Solution {
4    fn min_area(image: Vec<Vec<char>>, _x: i32, _y: i32) -> i32 {
5        let n = image.len();
6        let m = image[0].len();
7        let mut left = m;
8        let mut right = 0;
9        let mut top = n;
10        let mut bottom = 0;
11        for i in 0..n {
12            for j in 0..m {
13                if image[i][j] == '1' {
14                    left = left.min(j);
15                    right = right.max(j);
16                    top = top.min(i);
17                    bottom = bottom.max(i);
18                }
19            }
20        }
21        ((right - left + 1) * (bottom - top + 1)) as i32
22    }
23}
24
25#[test]
26fn test() {
27    let image = vec_vec_char![
28        ['0', '0', '1', '0'],
29        ['0', '1', '1', '0'],
30        ['0', '1', '0', '0']
31    ];
32    let x = 0;
33    let y = 2;
34    let res = 6;
35    assert_eq!(Solution::min_area(image, x, y), res);
36}