feat: 3195_find_the_minimum_area_to_cover_all_ones
This commit is contained in:
parent
7418f4f62c
commit
a8b5c56bbf
7
3195_find_the_minimum_area_to_cover_all_ones/Cargo.lock
generated
Normal file
7
3195_find_the_minimum_area_to_cover_all_ones/Cargo.lock
generated
Normal 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"
|
6
3195_find_the_minimum_area_to_cover_all_ones/Cargo.toml
Normal file
6
3195_find_the_minimum_area_to_cover_all_ones/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "find_the_minimum_area_to_cover_all_ones"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
54
3195_find_the_minimum_area_to_cover_all_ones/src/lib.rs
Normal file
54
3195_find_the_minimum_area_to_cover_all_ones/src/lib.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user