-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmaximalSquare.go
More file actions
67 lines (58 loc) · 1.2 KB
/
Copy pathmaximalSquare.go
File metadata and controls
67 lines (58 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* https://leetcode.com/problems/maximal-square/description/
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
For example, given the following matrix:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 4.
*/
package ldp
func maximalSquare(matrix [][]byte) int {
if len(matrix) == 0 || len(matrix[0]) == 0 {
return 0
}
row, col := len(matrix), len(matrix[0])
dp, side := make([][]int, row), 0
for i := range dp {
dp[i] = make([]int, col)
}
for j := 0; j < col; j++ {
if matrix[0][j] == '1' {
dp[0][j] = 1
side = 1
}
}
for i := 1; i < row; i++ {
if matrix[i][0] == '1' {
dp[i][0] = 1
side = 1
}
}
for i := 1; i < row; i++ {
for j := 1; j < col; j++ {
if matrix[i][j] == '0' {
continue
}
if dp[i-1][j] == 0 || dp[i][j-1] == 0 || dp[i-1][j-1] == 0 {
dp[i][j] = 1
} else if tmp := dp[i-1][j]; tmp == dp[i][j-1] {
if dp[i-tmp][j-tmp] != 0 {
dp[i][j] = tmp + 1
} else {
dp[i][j] = tmp
}
} else {
min := tmp
if dp[i][j-1] < min {
min = dp[i][j-1]
}
dp[i][j] = min + 1
}
if side < dp[i][j] {
side = dp[i][j]
}
}
}
return side * side
}