Skip to content

Commit 8d94712

Browse files
committed
feat: 1277. Count Square Submatrices with All Ones : DP
1 parent e5abe66 commit 8d94712

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @return {number}
4+
*/
5+
const countSquares = function (matrix) {
6+
const m = matrix.length; const n = matrix[0].length
7+
const dp = [...Array(m)].map(() => [])
8+
let ans = 0
9+
for (let i = 0; i < m; i++) {
10+
for (let j = 0; j < n; j++) {
11+
if (matrix[i][j] === 1) {
12+
dp[i][j] = Math.min(dp[i - 1]?.[j] ?? 0, dp[i]?.[j - 1] ?? 0, dp[i - 1]?.[j - 1] ?? 0) + 1
13+
ans += dp[i][j]
14+
} else { dp[i][j] = 0 }
15+
}
16+
}
17+
return ans
18+
}

Diff for: leetcode/残酷刷题/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
- 1751. Maximum Number of Events That Can Be Attended II
6969
- 1478. Allocate Mailboxes
7070
- DP + 货仓选址
71+
- 1277. Count Square Submatrices with All Ones
7172

7273
#### 状态压缩
7374

0 commit comments

Comments
 (0)