Skip to content

Commit d3889ce

Browse files
committed
一刷85
1 parent 3d89d27 commit d3889ce

File tree

7 files changed

+142
-22
lines changed

7 files changed

+142
-22
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -621,12 +621,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
621621
|Hard
622622
|
623623

624-
//|{counter:codes}
625-
//|{leetcode_base_url}/maximal-rectangle/[85. Maximal Rectangle^]
626-
//|{source_base_url}/_0085_MaximalRectangle.java[Java]
627-
//|{doc_base_url}/0085-maximal-rectangle.adoc[题解]
628-
//|Hard
629-
//|
624+
|{counter:codes}
625+
|{leetcode_base_url}/maximal-rectangle/[85. Maximal Rectangle^]
626+
|{source_base_url}/_0085_MaximalRectangle.java[Java]
627+
|{doc_base_url}/0085-maximal-rectangle.adoc[题解]
628+
|Hard
629+
|
630630

631631
|{counter:codes}
632632
|{leetcode_base_url}/partition-list/[86. Partition List^]

docs/0085-maximal-rectangle.adoc

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,80 @@
11
[#0085-maximal-rectangle]
2-
= 85. Maximal Rectangle
2+
= 85. 最大矩形
33

4-
{leetcode}/problems/maximal-rectangle/[LeetCode - Maximal Rectangle^]
4+
https://leetcode.cn/problems/maximal-rectangle/[LeetCode - 85. 最大矩形 ^]
55

6-
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
6+
给定一个仅包含 `0``1` 、大小为 `rows x cols` 的二维二进制矩阵,找出只包含 `1` 的最大矩形,并返回其面积。
77

8-
*Example:*
8+
*示例 1:*
99

10-
[subs="verbatim,quotes,macros"]
11-
----
12-
*Input:*
13-
[
14-
["1","0","1","0","0"],
15-
["1","0","*1*","*1*","*1*"],
16-
["1","1","*1*","*1*","*1*"],
17-
["1","0","0","1","0"]
18-
]
19-
*Output:* 6
20-
----
10+
image::images/0085-01.png[{image_attr}]
11+
12+
....
13+
输入:matrix = [
14+
["1","0","1","0","0"],
15+
["1","0","1","1","1"],
16+
["1","1","1","1","1"],
17+
["1","0","0","1","0"]
18+
]
19+
输出:6
20+
解释:最大矩形如上图所示。
21+
....
22+
23+
*示例 2:*
24+
25+
....
26+
输入:matrix = [["0"]]
27+
输出:0
28+
....
29+
30+
*示例 3:*
31+
32+
....
33+
输入:matrix = [["1"]]
34+
输出:1
35+
....
36+
37+
*提示:*
2138

39+
* `rows == matrix.length`
40+
* `cols == matrix[0].length`
41+
* `+1 <= row, cols <= 200+`
42+
* `matrix[i][j]``0``1`
43+
44+
45+
== 思路分析
46+
47+
还想暴力解决,奈何一团浆糊!
48+
49+
计算每一行的高度,利用这个高度,使用与 xref:0084-largest-rectangle-in-histogram.adoc[84. 柱状图中最大的矩形] 中单调栈的思路来解决这个问题!
50+
51+
image::images/0085-10.png[{image_attr}]
2252

2353
[[src-0085]]
54+
[tabs]
55+
====
56+
一刷::
57+
+
58+
--
2459
[{java_src_attr}]
2560
----
2661
include::{sourcedir}/_0085_MaximalRectangle.java[tag=answer]
2762
----
63+
--
64+
65+
// 二刷::
66+
// +
67+
// --
68+
// [{java_src_attr}]
69+
// ----
70+
// include::{sourcedir}/_0085_MaximalRectangle_2.java[tag=answer]
71+
// ----
72+
// --
73+
====
74+
75+
76+
== 参考资料
2877

78+
. https://leetcode.cn/problems/maximal-rectangle/solutions/9535/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-by-1-8/[85. 最大矩形 - 详细通俗的思路分析,多解法^] -- 解决非常细致!
79+
. https://leetcode.cn/problems/maximal-rectangle/solutions/535672/zui-da-ju-xing-by-leetcode-solution-bjlu/[85. 最大矩形 - 官方题解^]
80+
. https://leetcode.cn/problems/maximal-rectangle/solutions/535898/python3-qian-zhui-he-dan-diao-zhan-ji-su-vkpp/[85. 最大矩形 - 前缀和+单调栈计算最大矩形^]

docs/images/0085-01.png

18.3 KB
Loading

docs/images/0085-10.png

133 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ include::0083-remove-duplicates-from-sorted-list.adoc[leveloffset=+1]
253253

254254
include::0084-largest-rectangle-in-histogram.adoc[leveloffset=+1]
255255

256-
// include::0085-maximal-rectangle.adoc[leveloffset=+1]
256+
include::0085-maximal-rectangle.adoc[leveloffset=+1]
257257

258258
include::0086-partition-list.adoc[leveloffset=+1]
259259

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,11 @@ endif::[]
10581058
|{doc_base_url}/0044-wildcard-matching.adoc[题解]
10591059
|❌ 动态规划。尝试双指针失败!完全没有想到可以用动态规划来解决这个问题!
10601060

1061+
|{counter:codes2503}
1062+
|{leetcode_base_url}/maximal-rectangle/[85. 最大矩形^]
1063+
|{doc_base_url}/0085-maximal-rectangle.adoc[题解]
1064+
|❌ 单调栈!计算每一行的高度,然后利用这个高度,使用与 {leetcode_base_url}/largest-rectangle-in-histogram/[84. 柱状图中最大的矩形^] 中相同的思路,结合单调栈的模式来解决问题。多种解法,还有动态规划的解法。
1065+
10611066
|===
10621067

10631068
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
public class _0085_MaximalRectangle {
7+
// tag::answer[]
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2025-06-25 20:59:45
11+
*/
12+
public int maximalRectangle(char[][] matrix) {
13+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
14+
return 0;
15+
}
16+
int[] heights = new int[matrix[0].length];
17+
int result = 0;
18+
for (int row = 0; row < matrix.length; row++) {
19+
// 计算出每一行的高度
20+
for (int column = 0; column < matrix[row].length; column++) {
21+
if (matrix[row][column] == '1') {
22+
heights[column] += 1;
23+
} else {
24+
heights[column] = 0;
25+
}
26+
}
27+
// 利用每一行的高度,去计算可以组成的最大矩阵面积
28+
result = Math.max(result, largestRectangleArea(heights));
29+
}
30+
return result;
31+
}
32+
33+
private int largestRectangleArea(int[] heights) {
34+
int length = heights.length;
35+
if (length == 1) {
36+
return heights[0];
37+
}
38+
int[] newHeights = new int[length + 2];
39+
// 两端两个哨兵
40+
newHeights[0] = 0;
41+
newHeights[length + 1] = 0;
42+
System.arraycopy(heights, 0, newHeights, 1, length);
43+
heights = newHeights;
44+
Deque<Integer> stack = new ArrayDeque<>();
45+
stack.push(heights[0]);
46+
int result = 0;
47+
for (int i = 1; i < newHeights.length; i++) {
48+
while (heights[i] < heights[stack.peek()]) {
49+
Integer left = stack.pop();
50+
// 以弹出的栈顶元素为高,因为有可能一柱擎天就是最大的面积
51+
// 另外,弹出一个元素后,如果当前高度还是比栈顶元素低,
52+
// 那么下次再弹出时,就会一个更低的高度去计算从栈顶元素到 i-1 的矩阵了
53+
int hight = heights[left];
54+
// 因为 heights[i] 已经变低了,所以,得从 i-1 开始想前算
55+
int width = (i - 1) - stack.peek();
56+
result = Math.max(result, hight * width);
57+
}
58+
stack.push(i);
59+
}
60+
return result;
61+
}
62+
// end::answer[]
63+
}

0 commit comments

Comments
 (0)