feat: 1277_count_square_submatrices_with_all_ones
This commit is contained in:
parent
3995fd9dc2
commit
18d9c661e8
7
1277_count_square_submatrices_with_all_ones/Cargo.lock
generated
Normal file
7
1277_count_square_submatrices_with_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 = "count_square_submatrices_with_all_ones"
|
||||||
|
version = "0.1.0"
|
6
1277_count_square_submatrices_with_all_ones/Cargo.toml
Normal file
6
1277_count_square_submatrices_with_all_ones/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "count_square_submatrices_with_all_ones"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
29
1277_count_square_submatrices_with_all_ones/src/lib.rs
Normal file
29
1277_count_square_submatrices_with_all_ones/src/lib.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user