feat: 3195_find_the_minimum_area_to_cover_all_ones

This commit is contained in:
SquidSpirit 2025-08-22 16:19:10 +08:00
parent 7418f4f62c
commit a8b5c56bbf
3 changed files with 67 additions and 0 deletions

View File

@ -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"

View File

@ -0,0 +1,6 @@
[package]
name = "find_the_minimum_area_to_cover_all_ones"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -0,0 +1,54 @@
pub struct Solution;
impl Solution {
pub fn minimum_area(grid: Vec<Vec<i32>>) -> 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
}
}