Skip to content

Commit 8a72eb0

Browse files
committed
Add problem 3195: Find the Minimum Area to Cover All Ones I
1 parent 98595e8 commit 8a72eb0

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,6 +2131,7 @@ pub mod problem_3190_find_minimum_operations_to_make_all_elements_divisible_by_t
21312131
pub mod problem_3191_minimum_operations_to_make_binary_array_elements_equal_to_one_i;
21322132
pub mod problem_3192_minimum_operations_to_make_binary_array_elements_equal_to_one_ii;
21332133
pub mod problem_3194_minimum_average_of_smallest_and_largest_elements;
2134+
pub mod problem_3195_find_the_minimum_area_to_cover_all_ones_i;
21342135
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21352136

21362137
#[cfg(test)]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
use std::convert;
6+
7+
impl Solution {
8+
pub fn minimum_area(grid: Vec<Vec<i32>>) -> i32 {
9+
let mut min_row = u32::MAX;
10+
let mut max_row = 0;
11+
let mut min_column = u32::MAX;
12+
let mut max_column = 0;
13+
14+
(0..).zip(grid).for_each(|(y, row)| {
15+
let mut iter = row.iter().map(|&num| num != 0);
16+
17+
if let Some(last_column) = iter.rposition(convert::identity) {
18+
let first_column = iter.position(convert::identity).unwrap_or(last_column);
19+
20+
min_row = min_row.min(y);
21+
max_row = y;
22+
min_column = min_column.min(first_column as _);
23+
max_column = max_column.max(last_column as _);
24+
}
25+
});
26+
27+
((max_column - min_column + 1) * (max_row - min_row + 1)).cast_signed()
28+
}
29+
}
30+
31+
// ------------------------------------------------------ snip ------------------------------------------------------ //
32+
33+
impl super::Solution for Solution {
34+
fn minimum_area(grid: Vec<Vec<i32>>) -> i32 {
35+
Self::minimum_area(grid)
36+
}
37+
}
38+
39+
#[cfg(test)]
40+
mod tests {
41+
#[test]
42+
fn test_solution() {
43+
super::super::tests::run::<super::Solution>();
44+
}
45+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
pub mod greedy;
2+
3+
pub trait Solution {
4+
fn minimum_area(grid: Vec<Vec<i32>>) -> 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 = [(&[[0, 1, 0], [1, 0, 1]] as &dyn Matrix<_>, 6), (&[[1, 0], [0, 0]], 1)];
14+
15+
for (grid, expected) in test_cases {
16+
assert_eq!(S::minimum_area(grid.to_vec()), expected);
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)