feat: 1277_count_square_submatrices_with_all_ones

This commit is contained in:
SquidSpirit 2025-08-20 22:09:32 +08:00
parent 3995fd9dc2
commit 18d9c661e8
3 changed files with 42 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 = "count_square_submatrices_with_all_ones"
version = "0.1.0"

View File

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

View File

@ -0,0 +1,29 @@
pub struct Solution;
impl Solution {
pub fn count_squares(mut matrix: Vec<Vec<i32>>) -> i32 {
let m = matrix.len();
let n = matrix[0].len();
let mut result = 0;
for i in 0..m {
for j in 0..n {
if matrix[i][j] == 1 {
if i > 0 && j > 0 {
let available_size =
*[matrix[i - 1][j - 1], matrix[i - 1][j], matrix[i][j - 1]]
.iter()
.min()
.unwrap();
matrix[i][j] += available_size;
}
result += matrix[i][j];
}
}
}
result
}
}