From cfe76225290e4a1f6f0e7ebf15f21436abd07eef Mon Sep 17 00:00:00 2001 From: chayan das Date: Thu, 21 Aug 2025 21:40:20 +0530 Subject: [PATCH] Create 1504. Count Submatrices With All Ones --- 1504. Count Submatrices With All Ones | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 1504. Count Submatrices With All Ones diff --git a/1504. Count Submatrices With All Ones b/1504. Count Submatrices With All Ones new file mode 100644 index 0000000..141bed7 --- /dev/null +++ b/1504. Count Submatrices With All Ones @@ -0,0 +1,21 @@ +class Solution { +public: + int numSubmat(vector>& mat) { + int m = mat.size(), n = mat[0].size(), ans = 0; + vector heights(n, 0); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (mat[i][j] == 0) heights[j] = 0; + else heights[j] += 1; + } + for (int j = 0; j < n; j++) { + int minHeight = heights[j]; + for (int k = j; k >= 0 && minHeight > 0; k--) { + minHeight = min(minHeight, heights[k]); + ans += minHeight; + } + } + } + return ans; + } +};