Skip to content

Commit 96d5f54

Browse files
aQuaaQua
aQua
authored and
aQua
committed
85 Time Limit Exceeded
1 parent 65e2e1d commit 96d5f54

File tree

3 files changed

+332
-0
lines changed

3 files changed

+332
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# [85. Maximal Rectangle](https://leetcode.com/problems/maximal-rectangle/)
2+
3+
## 题目
4+
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
5+
6+
For example, given the following matrix:
7+
```
8+
1 0 1 0 0
9+
1 0 1 1 1
10+
1 1 1 1 1
11+
1 0 0 1 0
12+
```
13+
Return 6.
14+
15+
## 解题思路
16+
17+
见程序注释
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package Problem0085
2+
3+
func maximalRectangle(mat [][]byte) int {
4+
m := len(mat)
5+
if m == 0 {
6+
return 0
7+
}
8+
n := len(mat[0])
9+
if n == 0 {
10+
return 0
11+
}
12+
13+
check := func(ax, ay, bx, by int) int {
14+
for i := ax; i <= bx; i++ {
15+
for j := ay; j <= by; j++ {
16+
if mat[i][j] == '0' {
17+
return 0
18+
}
19+
}
20+
}
21+
return (bx - ax + 1) * (by - ay + 1)
22+
}
23+
24+
max := 0
25+
for ax := 0; ax < m; ax++ {
26+
for ay := 0; ay < n; ay++ {
27+
for bx := ax; bx < m; bx++ {
28+
for by := ay; by < n; by++ {
29+
tmp := check(ax, ay, bx, by)
30+
if max < tmp {
31+
max = tmp
32+
}
33+
}
34+
}
35+
}
36+
}
37+
38+
return max
39+
}

0 commit comments

Comments
 (0)