Skip to content

Commit 873aae2

Browse files
committed
二刷84
1 parent ef7a87e commit 873aae2

File tree

4 files changed

+63
-11
lines changed

4 files changed

+63
-11
lines changed

docs/0000-10-monotonic-stack.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ image::images/0739-10.png[{image_attr}]
5656
== 经典题目
5757

5858
. xref:0042-trapping-rain-water.adoc[42. Trapping Rain Water] -- 这个单调栈,在中间过程加了一些操作,有意思。
59-
. xref:0084-largest-rectangle-in-histogram.adoc[84. Largest Rectangle in Histogram]
59+
. xref:0084-largest-rectangle-in-histogram.adoc[84. Largest Rectangle in Histogram] -- 在出栈循环中处理逻辑。
6060
. xref:0085-maximal-rectangle.adoc[85. Maximal Rectangle]
6161
. xref:0155-min-stack.adoc[155. Min Stack] -- 这个辅助栈也可以认为是一种单调栈。
6262
. xref:0255-verify-preorder-sequence-in-binary-search-tree.adoc[255. Verify Preorder Sequence in Binary Search Tree]

docs/0084-largest-rectangle-in-histogram.adoc

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[#0084-largest-rectangle-in-histogram]
22
= 84. 柱状图中最大的矩形
33

4-
https://leetcode.cn/problems/largest-rectangle-in-histogram/[LeetCode - 84. 柱状图中最大的矩形 ^]
4+
https://leetcode.cn/problems/largest-rectangle-in-histogram/[LeetCode - 84. 柱状图中最大的矩形^]
55

6-
给定 _n_ 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 `1`
6+
给定 `n` 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1
77

88
求在该柱状图中,能够勾勒出来的矩形的最大面积。
99

@@ -30,12 +30,14 @@ image:images/0084-04.jpg[]
3030
输出: 4
3131
....
3232

33+
3334
*提示:*
3435

3536
* `1 \<= heights.length \<=10^5^`
3637
* `0 \<= heights[i] \<= 10^4^`
3738
3839
40+
3941
== 思路分析
4042

4143
image::images/0084-10.png[{image_attr}]
@@ -59,18 +61,20 @@ include::{sourcedir}/_0084_LargestRectangleInHistogram.java[tag=answer]
5961
----
6062
--
6163
62-
// 二刷::
63-
// +
64-
// --
65-
// [{java_src_attr}]
66-
// ----
67-
// include::{sourcedir}/_0084_LargestRectangleInHistogram_2.java[tag=answer]
68-
// ----
69-
// --
64+
二刷::
65+
+
66+
--
67+
[{java_src_attr}]
68+
----
69+
include::{sourcedir}/_0084_LargestRectangleInHistogram_2.java[tag=answer]
70+
----
71+
--
7072
====
7173

7274

7375
== 参考资料
7476

7577
. https://leetcode.cn/problems/largest-rectangle-in-histogram/solutions/142012/bao-li-jie-fa-zhan-by-liweiwei1419/[84. 柱状图中最大的矩形 - 暴力解法、栈(单调栈、哨兵技巧)^] -- 哨兵技巧牛逼
7678
. https://leetcode.cn/problems/largest-rectangle-in-histogram/solutions/266844/zhu-zhuang-tu-zhong-zui-da-de-ju-xing-by-leetcode-/[84. 柱状图中最大的矩形 - 官方题解^] -- 看的稀里糊涂。
79+
. https://leetcode.cn/problems/largest-rectangle-in-histogram/solutions/2695467/dan-diao-zhan-fu-ti-dan-pythonjavacgojsr-89s7/[84. 柱状图中最大的矩形 - 一步步优化:从三次遍历到一次遍历^]
80+
. https://leetcode.cn/problems/largest-rectangle-in-histogram/solutions/108083/84-by-ikaruga/[84. 柱状图中最大的矩形 - 单调栈入门,使用单调栈快速寻找边界^]

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,6 +1909,11 @@ endif::[]
19091909
|{doc_base_url}/0094-binary-tree-inorder-traversal.adoc[题解]
19101910
|✅ 深度优先+栈。递归法非常简单;迭代法则是用栈来模拟递归;而“变形”莫尔斯解法则非常有趣。
19111911

1912+
|{counter:codes2503}
1913+
|{leetcode_base_url}/largest-rectangle-in-histogram/[84. 柱状图中最大的矩形^]
1914+
|{doc_base_url}/0084-largest-rectangle-in-histogram.adoc[题解]
1915+
|⭕️ 单调栈。哨兵技巧非常巧妙,不仅仅一边可以加哨兵,两边都可以加哨兵。另外,弹出的栈顶元素两边(栈顶元素,当前遍历元素)都是比栈顶低的元素,所以,取栈顶元素和“两边夹层”就是可以得到的最大矩形。
1916+
19121917
|===
19131918

19141919
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
public class _0084_LargestRectangleInHistogram_2 {
7+
// tag::answer[]
8+
9+
/**
10+
* @author D瓜哥 · https://www.diguage.com
11+
* @since 2025-11-14 21:21:27
12+
*/
13+
public int largestRectangleArea(int[] heights) {
14+
int[] temp = new int[heights.length + 2];
15+
// 两个哨兵,推动把整个数组处理完
16+
temp[0] = 0;
17+
temp[temp.length - 1] = 0;
18+
System.arraycopy(heights, 0, temp, 1, heights.length);
19+
heights = temp;
20+
Deque<Integer> stack = new ArrayDeque<>();
21+
stack.push(0);
22+
int result = 0;
23+
for (int i = 1; i < heights.length; i++) {
24+
while (heights[i] < heights[stack.peek()]) {
25+
// 弹出的栈顶元素最高
26+
int height = heights[stack.pop()];
27+
// 两边(当前栈顶元素,当前遍历元素)都是比它低的
28+
// 左边:肯定都是比栈顶元素高;如果有更低的,当前元素早就被弹出了
29+
// 右边:当前遍历元素肯定比最近弹出的栈顶元素低。高的话,直接压栈了。
30+
int width = i - stack.peek() - 1;
31+
result = Math.max(result, height * width);
32+
}
33+
stack.push(i);
34+
}
35+
return result;
36+
}
37+
38+
// end::answer[]
39+
static void main() {
40+
new _0084_LargestRectangleInHistogram_2()
41+
.largestRectangleArea(new int[]{2, 1, 2});
42+
}
43+
}

0 commit comments

Comments
 (0)