Skip to content

Commit 82ede79

Browse files
committed
feat: 2088 Count Fertile Pyramids in a Land : DP
1 parent 8d94712 commit 82ede79

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- Google KickStart [kick-start](kick-start)
1515
- LeetCode [leetcode](leetcode)
1616
- [残酷刷题](leetcode/残酷刷题)
17+
- 想加入残酷刷题群的朋友可以联系我,[残酷刷题群2020年会总结](https://zhuanlan.zhihu.com/p/341323903)[群规链接](http://board.cruelcoding.com/rules.html)
1718
- 伪代码 [pseudoCode](pseudoCode)
1819
- 其他的都是之前刷 PAT 时候的题目
1920

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function countPyramids (grid: number[][]): number {
2+
const [m, n] = [grid.length, grid[0].length]
3+
let ans = 0
4+
const dpUp: number[][] = []
5+
const dpDown: number[][] = []
6+
7+
const [left, right]: [number[][], number[][]] = [[], []]
8+
for (let i = 0; i < m; i++) {
9+
left[i] = []; right[i] = []
10+
for (let j = 0; j < n; j++) {
11+
if (grid[i][j] === 1) left[i][j] = (left[i][j - 1] ?? 0) + 1
12+
else left[i][j] = 0
13+
}
14+
for (let j = n - 1; j >= 0; j--) {
15+
if (grid[i][j] === 1) right[i][j] = (right[i][j + 1] ?? 0) + 1
16+
else right[i][j] = 0
17+
}
18+
}
19+
for (let i = 0; i < m; i++) {
20+
dpUp[i] = []
21+
for (let j = 0; j < n; j++) {
22+
dpUp[i][j] = Math.min((dpUp[i - 1]?.[j] ?? 0) + 1, left[i][j], right[i][j])
23+
if (dpUp[i][j] > 1) ans += dpUp[i][j] - 1
24+
}
25+
}
26+
for (let i = m - 1; i >= 0; i--) {
27+
dpDown[i] = []
28+
for (let j = 0; j < n; j++) {
29+
dpDown[i][j] = Math.min((dpDown[i + 1]?.[j] ?? 0) + 1, left[i][j], right[i][j])
30+
if (dpDown[i][j] > 1) ans += dpDown[i][j] - 1
31+
}
32+
}
33+
return ans
34+
};

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

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
- 1478. Allocate Mailboxes
7070
- DP + 货仓选址
7171
- 1277. Count Square Submatrices with All Ones
72+
- 2088. Count Fertile Pyramids in a Land
7273

7374
#### 状态压缩
7475

0 commit comments

Comments
 (0)