Skip to content

Commit f95e194

Browse files
committed
feat: 最大正方形
1 parent 16fa5cc commit f95e194

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

动态规划/恢复空格-面试题 17.13.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ let respace = function (dictionary, sentence) {
1616
dp[i] = min
1717
}
1818
return dp[n]
19-
}
19+
}

动态规划/最大正方形-221.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @param {character[][]} matrix
3+
* @return {number}
4+
*/
5+
let maximalSquare = function (matrix) {
6+
let maxY = matrix.length
7+
if (!maxY) return 0
8+
let maxX = matrix[0].length
9+
10+
let dp = []
11+
let max = 0
12+
13+
let dpBasic = (y, x) => {
14+
if (matrix[y][x] === "1") {
15+
max = 1
16+
dp[y][x] = 1
17+
} else {
18+
dp[y][x] = 0
19+
}
20+
}
21+
for (let y = 0; y < maxY; y++) {
22+
dp[y] = []
23+
dpBasic(y, 0)
24+
}
25+
for (let x = 1; x < maxX; x++) {
26+
dpBasic(0, x)
27+
}
28+
29+
for (let y = 1; y < maxY; y++) {
30+
for (let x = 1; x < maxX; x++) {
31+
let val = matrix[y][x]
32+
if (val === "0") {
33+
dp[y][x] = 0
34+
} else {
35+
let left = dp[y][x - 1]
36+
let top = dp[y - 1][x]
37+
let leftTop = dp[y - 1][x - 1]
38+
dp[y][x] = Math.min(left, top, leftTop) + 1
39+
max = Math.max(max, dp[y][x])
40+
}
41+
}
42+
}
43+
return max * max
44+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy