Skip to content

Commit cc1d44e

Browse files
committed
maximal_square
1 parent 983afbf commit cc1d44e

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
177177
#### [217. Contains Duplicate](https://github.com/hitzzc/go-leetcode/tree/master/contains_duplicate)
178178
#### [218. Contains Duplicate II](https://github.com/hitzzc/go-leetcode/tree/master/contains_duplicate_II)
179179
#### [219. Contains Duplicate III (unsolved)](https://github.com/hitzzc/go-leetcode/tree/master/contains_duplicate_III)
180+
#### [221. Maximal Square](https://github.com/hitzzc/go-leetcode/tree/master/maximal_square)
180181

181182

182183

maximal_square/maximal_square.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package maximal_square
2+
3+
func maximalSquare(matrix [][]byte) int {
4+
if len(matrix) == 0 || len(matrix[0]) == 0 {
5+
return 0
6+
}
7+
m := map[byte]int{
8+
'0': 0,
9+
'1': 1,
10+
}
11+
p := make([][]int, len(matrix))
12+
for i := range p {
13+
p[i] = make([]int, len(matrix[i]))
14+
}
15+
max := 0
16+
for i := range matrix {
17+
for j := range matrix[i] {
18+
if i == 0 || j == 0 {
19+
p[i][j] = m[matrix[i][j]]
20+
} else if matrix[i][j] == '1' {
21+
p[i][j] = min(min(p[i-1][j], p[i][j-1]), p[i-1][j-1]) + 1
22+
}
23+
if p[i][j] > max {
24+
max = p[i][j]
25+
}
26+
}
27+
}
28+
return max * max
29+
}
30+
31+
func min(i, j int) int {
32+
if i < j {
33+
return i
34+
}
35+
return j
36+
}

0 commit comments

Comments
 (0)