Skip to content

Commit 9b3440a

Browse files
committed
+ problem 85
1 parent 6379893 commit 9b3440a

File tree

5 files changed

+179
-0
lines changed

5 files changed

+179
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# 85. Maximal Rectangle
2+
Given a `rows x cols` binary `matrix` filled with `0`'s and `1`'s, find the largest rectangle containing only `1`'s and return *its area*.
3+
4+
#### Example 1:
5+
![](https://assets.leetcode.com/uploads/2020/09/14/maximal.jpg)
6+
<pre>
7+
<strong>Input:</strong> matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
8+
<strong>Output:</strong> 6
9+
<strong>Explanation:</strong> The maximal rectangle is shown in the above picture.
10+
</pre>
11+
12+
#### Example 2:
13+
<pre>
14+
<strong>Input:</strong> matrix = [["0"]]
15+
<strong>Output:</strong> 0
16+
</pre>
17+
18+
#### Example 3:
19+
<pre>
20+
<strong>Input:</strong> matrix = [["1"]]
21+
<strong>Output:</strong> 1
22+
</pre>
23+
24+
#### Constraints:
25+
* `rows == matrix.length`
26+
* `cols == matrix[i].length`
27+
* `1 <= row, cols <= 200`
28+
* `matrix[i][j]` is `'0'` or `'1'`.
29+
30+
## Solutions (Rust)
31+
32+
### 1. Solution
33+
```Rust
34+
impl Solution {
35+
pub fn maximal_rectangle(matrix: Vec<Vec<char>>) -> i32 {
36+
let rows = matrix.len();
37+
let cols = matrix[0].len();
38+
let mut prefix_sum = vec![vec![0; cols]; rows];
39+
let mut ret = 0;
40+
41+
for r in 0..rows {
42+
for c in 0..cols {
43+
if matrix[r][c] == '0' {
44+
continue;
45+
}
46+
47+
prefix_sum[r][c] = 1;
48+
49+
if c > 0 {
50+
prefix_sum[r][c] += prefix_sum[r][c - 1];
51+
}
52+
53+
let mut min_w = i32::MAX;
54+
55+
for h in 1..=r + 1 {
56+
if prefix_sum[r + 1 - h][c] == 0 {
57+
break;
58+
}
59+
60+
min_w = min_w.min(prefix_sum[r + 1 - h][c]);
61+
ret = ret.max(min_w * h as i32);
62+
}
63+
}
64+
}
65+
66+
ret
67+
}
68+
}
69+
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# 85. 最大矩形
2+
给定一个仅包含 `0``1` 、大小为 `rows x cols` 的二维二进制矩阵,找出只包含 `1` 的最大矩形,并返回其面积。
3+
4+
#### 示例 1:
5+
![](https://assets.leetcode.com/uploads/2020/09/14/maximal.jpg)
6+
<pre>
7+
<strong>输入:</strong> matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
8+
<strong>输出:</strong> 6
9+
<strong>解释:</strong> 最大矩形如上图所示。
10+
</pre>
11+
12+
#### 示例 2:
13+
<pre>
14+
<strong>输入:</strong> matrix = [["0"]]
15+
<strong>输出:</strong> 0
16+
</pre>
17+
18+
#### 示例 3:
19+
<pre>
20+
<strong>输入:</strong> matrix = [["1"]]
21+
<strong>输出:</strong> 1
22+
</pre>
23+
24+
#### 提示:
25+
* `rows == matrix.length`
26+
* `cols == matrix[i].length`
27+
* `1 <= row, cols <= 200`
28+
* `matrix[i][j]``'0'``'1'`
29+
30+
## 题解 (Rust)
31+
32+
### 1. 题解
33+
```Rust
34+
impl Solution {
35+
pub fn maximal_rectangle(matrix: Vec<Vec<char>>) -> i32 {
36+
let rows = matrix.len();
37+
let cols = matrix[0].len();
38+
let mut prefix_sum = vec![vec![0; cols]; rows];
39+
let mut ret = 0;
40+
41+
for r in 0..rows {
42+
for c in 0..cols {
43+
if matrix[r][c] == '0' {
44+
continue;
45+
}
46+
47+
prefix_sum[r][c] = 1;
48+
49+
if c > 0 {
50+
prefix_sum[r][c] += prefix_sum[r][c - 1];
51+
}
52+
53+
let mut min_w = i32::MAX;
54+
55+
for h in 1..=r + 1 {
56+
if prefix_sum[r + 1 - h][c] == 0 {
57+
break;
58+
}
59+
60+
min_w = min_w.min(prefix_sum[r + 1 - h][c]);
61+
ret = ret.max(min_w * h as i32);
62+
}
63+
}
64+
}
65+
66+
ret
67+
}
68+
}
69+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
impl Solution {
2+
pub fn maximal_rectangle(matrix: Vec<Vec<char>>) -> i32 {
3+
let rows = matrix.len();
4+
let cols = matrix[0].len();
5+
let mut prefix_sum = vec![vec![0; cols]; rows];
6+
let mut ret = 0;
7+
8+
for r in 0..rows {
9+
for c in 0..cols {
10+
if matrix[r][c] == '0' {
11+
continue;
12+
}
13+
14+
prefix_sum[r][c] = 1;
15+
16+
if c > 0 {
17+
prefix_sum[r][c] += prefix_sum[r][c - 1];
18+
}
19+
20+
let mut min_w = i32::MAX;
21+
22+
for h in 1..=r + 1 {
23+
if prefix_sum[r + 1 - h][c] == 0 {
24+
break;
25+
}
26+
27+
min_w = min_w.min(prefix_sum[r + 1 - h][c]);
28+
ret = ret.max(min_w * h as i32);
29+
}
30+
}
31+
}
32+
33+
ret
34+
}
35+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
[81][81l] |[Search in Rotated Sorted Array II][81] |![rs]
6969
[82][82l] |[Remove Duplicates from Sorted List II][82] |![rb]
7070
[83][83l] |[Remove Duplicates from Sorted List][83] |![py]
71+
[85][85l] |[Maximal Rectangle][85] |![rs]
7172
[86][86l] |[Partition List][86] |![py]
7273
[88][88l] |[Merge Sorted Array][88] |![py]&nbsp;&nbsp;![rb]
7374
[89][89l] |[Gray Code][89] |![rs]
@@ -1399,6 +1400,7 @@
13991400
[81]:Problemset/0081-Search%20in%20Rotated%20Sorted%20Array%20II/README.md#81-search-in-rotated-sorted-array-ii
14001401
[82]:Problemset/0082-Remove%20Duplicates%20from%20Sorted%20List%20II/README.md#82-remove-duplicates-from-sorted-list-ii
14011402
[83]:Problemset/0083-Remove%20Duplicates%20from%20Sorted%20List/README.md#83-remove-duplicates-from-sorted-list
1403+
[85]:Problemset/0085-Maximal%20Rectangle/README.md#85-maximal-rectangle
14021404
[86]:Problemset/0086-Partition%20List/README.md#86-partition-list
14031405
[88]:Problemset/0088-Merge%20Sorted%20Array/README.md#88-merge-sorted-array
14041406
[89]:Problemset/0089-Gray%20Code/README.md#89-gray-code
@@ -2723,6 +2725,7 @@
27232725
[81l]:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/
27242726
[82l]:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
27252727
[83l]:https://leetcode.com/problems/remove-duplicates-from-sorted-list/
2728+
[85l]:https://leetcode.com/problems/maximal-rectangle/
27262729
[84l]:https://leetcode.com/problems/largest-rectangle-in-histogram/
27272730
[86l]:https://leetcode.com/problems/partition-list/
27282731
[88l]:https://leetcode.com/problems/merge-sorted-array/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
[81][81l] |[搜索旋转排序数组 II][81] |![rs]
6969
[82][82l] |[删除排序链表中的重复元素 II][82] |![rb]
7070
[83][83l] |[删除排序链表中的重复元素][83] |![py]
71+
[85][85l] |[最大矩形][85] |![rs]
7172
[86][86l] |[分隔链表][86] |![py]
7273
[88][88l] |[合并两个有序数组][88] |![py]&nbsp;&nbsp;![rb]
7374
[89][89l] |[格雷编码][89] |![rs]
@@ -1399,6 +1400,7 @@
13991400
[81]:Problemset/0081-Search%20in%20Rotated%20Sorted%20Array%20II/README_CN.md#81-搜索旋转排序数组-ii
14001401
[82]:Problemset/0082-Remove%20Duplicates%20from%20Sorted%20List%20II/README_CN.md#82-删除排序链表中的重复元素-ii
14011402
[83]:Problemset/0083-Remove%20Duplicates%20from%20Sorted%20List/README_CN.md#83-删除排序链表中的重复元素
1403+
[85]:Problemset/0085-Maximal%20Rectangle/README_CN.md#85-最大矩形
14021404
[86]:Problemset/0086-Partition%20List/README_CN.md#86-分隔链表
14031405
[88]:Problemset/0088-Merge%20Sorted%20Array/README_CN.md#88-合并两个有序数组
14041406
[89]:Problemset/0089-Gray%20Code/README_CN.md#89-格雷编码
@@ -2723,6 +2725,7 @@
27232725
[81l]:https://leetcode.cn/problems/search-in-rotated-sorted-array-ii/
27242726
[82l]:https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/
27252727
[83l]:https://leetcode.cn/problems/remove-duplicates-from-sorted-list/
2728+
[85l]:https://leetcode.cn/problems/maximal-rectangle/
27262729
[84l]:https://leetcode.cn/problems/largest-rectangle-in-histogram/
27272730
[86l]:https://leetcode.cn/problems/partition-list/
27282731
[88l]:https://leetcode.cn/problems/merge-sorted-array/

0 commit comments

Comments
 (0)