Skip to content

Commit c3e1271

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

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
use std::convert;
6+
7+
impl Solution {
8+
fn get_range(mut iter: impl DoubleEndedIterator<Item = bool> + ExactSizeIterator) -> u32 {
9+
iter.rposition(convert::identity).map_or(0, |right| {
10+
iter.position(convert::identity)
11+
.map_or(1, |left| right as u32 - left as u32 + 1)
12+
})
13+
}
14+
15+
pub fn minimum_area(grid: Vec<Vec<i32>>) -> i32 {
16+
let h_size = Self::get_range((0..grid.first().map_or(0, Vec::len)).map(|x| grid.iter().any(|row| row[x] != 0)));
17+
let v_size = Self::get_range(grid.into_iter().map(|row| row.iter().any(|&value| value != 0)));
18+
19+
(h_size * v_size).cast_signed()
20+
}
21+
}
22+
23+
// ------------------------------------------------------ snip ------------------------------------------------------ //
24+
25+
impl super::Solution for Solution {
26+
fn minimum_area(grid: Vec<Vec<i32>>) -> i32 {
27+
Self::minimum_area(grid)
28+
}
29+
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
#[test]
34+
fn test_solution() {
35+
super::super::tests::run::<super::Solution>();
36+
}
37+
}
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)