From 18d9c661e8fda80fb48d7d151ee88e5e66a10d65 Mon Sep 17 00:00:00 2001 From: SquidSpirit Date: Wed, 20 Aug 2025 22:09:32 +0800 Subject: [PATCH] feat: 1277_count_square_submatrices_with_all_ones --- .../Cargo.lock | 7 +++++ .../Cargo.toml | 6 ++++ .../src/lib.rs | 29 +++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 1277_count_square_submatrices_with_all_ones/Cargo.lock create mode 100644 1277_count_square_submatrices_with_all_ones/Cargo.toml create mode 100644 1277_count_square_submatrices_with_all_ones/src/lib.rs diff --git a/1277_count_square_submatrices_with_all_ones/Cargo.lock b/1277_count_square_submatrices_with_all_ones/Cargo.lock new file mode 100644 index 0000000..ff732b0 --- /dev/null +++ b/1277_count_square_submatrices_with_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 = "count_square_submatrices_with_all_ones" +version = "0.1.0" diff --git a/1277_count_square_submatrices_with_all_ones/Cargo.toml b/1277_count_square_submatrices_with_all_ones/Cargo.toml new file mode 100644 index 0000000..b236f40 --- /dev/null +++ b/1277_count_square_submatrices_with_all_ones/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "count_square_submatrices_with_all_ones" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/1277_count_square_submatrices_with_all_ones/src/lib.rs b/1277_count_square_submatrices_with_all_ones/src/lib.rs new file mode 100644 index 0000000..e9c3b42 --- /dev/null +++ b/1277_count_square_submatrices_with_all_ones/src/lib.rs @@ -0,0 +1,29 @@ +pub struct Solution; + +impl Solution { + pub fn count_squares(mut matrix: Vec>) -> 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 + } +}