diff --git a/3195_find_the_minimum_area_to_cover_all_ones/Cargo.lock b/3195_find_the_minimum_area_to_cover_all_ones/Cargo.lock new file mode 100644 index 0000000..9c0b72f --- /dev/null +++ b/3195_find_the_minimum_area_to_cover_all_ones/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "find_the_minimum_area_to_cover_all_ones" +version = "0.1.0" diff --git a/3195_find_the_minimum_area_to_cover_all_ones/Cargo.toml b/3195_find_the_minimum_area_to_cover_all_ones/Cargo.toml new file mode 100644 index 0000000..0990394 --- /dev/null +++ b/3195_find_the_minimum_area_to_cover_all_ones/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "find_the_minimum_area_to_cover_all_ones" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/3195_find_the_minimum_area_to_cover_all_ones/src/lib.rs b/3195_find_the_minimum_area_to_cover_all_ones/src/lib.rs new file mode 100644 index 0000000..c0a33c0 --- /dev/null +++ b/3195_find_the_minimum_area_to_cover_all_ones/src/lib.rs @@ -0,0 +1,54 @@ +pub struct Solution; + +impl Solution { + pub fn minimum_area(grid: Vec>) -> i32 { + let m = grid.len(); + let n = grid[0].len(); + + let top = (|| { + for i in 0..m { + for j in 0..n { + if grid[i][j] == 1 { + return i; + } + } + } + m - 1 + })(); + + let left = (|| { + for j in 0..n { + for i in top..m { + if grid[i][j] == 1 { + return j; + } + } + } + n - 1 + })(); + + let bottom = (|| { + for i in (top..m).rev() { + for j in left..n { + if grid[i][j] == 1 { + return i; + } + } + } + top + })(); + + let right = (|| { + for j in (left..n).rev() { + for i in top..(bottom + 1) { + if grid[i][j] == 1 { + return j; + } + } + } + left + })(); + + ((bottom - top + 1) * (right - left + 1)) as i32 + } +}