Skip to content

Commit 7102102

Browse files
committed
一刷84
1 parent 0d909c0 commit 7102102

File tree

7 files changed

+119
-35
lines changed

7 files changed

+119
-35
lines changed

README.adoc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -614,13 +614,13 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
614614
|Easy
615615
|
616616

617-
//|{counter:codes}
618-
//|{leetcode_base_url}/largest-rectangle-in-histogram/[84. Largest Rectangle in Histogram^]
619-
//|{source_base_url}/_0084_LargestRectangleInHistogram.java[Java]
620-
//|{doc_base_url}/0084-largest-rectangle-in-histogram.adoc[题解]
621-
//|Hard
622-
//|
623-
//
617+
|{counter:codes}
618+
|{leetcode_base_url}/largest-rectangle-in-histogram/[84. Largest Rectangle in Histogram^]
619+
|{source_base_url}/_0084_LargestRectangleInHistogram.java[Java]
620+
|{doc_base_url}/0084-largest-rectangle-in-histogram.adoc[题解]
621+
|Hard
622+
|
623+
624624
//|{counter:codes}
625625
//|{leetcode_base_url}/maximal-rectangle/[85. Maximal Rectangle^]
626626
//|{source_base_url}/_0085_MaximalRectangle.java[Java]
Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,76 @@
11
[#0084-largest-rectangle-in-histogram]
2-
= 84. Largest Rectangle in Histogram
2+
= 84. 柱状图中最大的矩形
33

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

6-
Given _n_ non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
6+
给定 _n_ 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 `1`
77

8-
9-
*Example 1:*
8+
求在该柱状图中,能够勾勒出来的矩形的最大面积。
109

1110
image::images/0084-00.png[{image_attr}]
1211

13-
[.small]#Above is a histogram where width of each bar is 1, given height = `[2,1,5,6,2,3]`.#
14-
1512
image::images/0084-01.png[{image_attr}]
1613

17-
[.small]#The largest rectangle is shown in the shaded area, which has area = `10` unit.#
18-
19-
[subs="verbatim,quotes,macros"]
20-
----
21-
*Input:* [2,1,5,6,2,3]
22-
*Output:* 10
23-
----
24-
25-
*Example 2:*
14+
*示例 1:*
2615

2716
image:images/0084-03.jpg[]
2817

29-
----
30-
Input: heights = [2,1,5,6,2,3]
31-
Output: 10
32-
Explanation: The above is a histogram where width of each bar is 1.
33-
The largest rectangle is shown in the red area, which has an area = 10 units.
34-
----
18+
....
19+
输入:heights = [2,1,5,6,2,3]
20+
输出:10
21+
解释:最大的矩形为图中红色区域,面积为 10
22+
....
3523

36-
*Example 3:*
24+
*示例 2:*
3725

3826
image:images/0084-04.jpg[]
3927

40-
----
41-
Input: heights = [2,4]
42-
Output: 4
43-
----
28+
....
29+
输入: heights = [2,4]
30+
输出: 4
31+
....
32+
33+
*提示:*
34+
35+
* `1 \<= heights.length \<=10^5^`
36+
* `0 \<= heights[i] \<= 10^4^`
37+
38+
39+
== 思路分析
40+
41+
image::images/0084-10.png[{image_attr}]
42+
43+
单调递增栈
44+
45+
当需要出栈的时候,就是当前元素小于栈顶元素。这样,弹出栈顶元素,以弹出的栈顶元素作为高,由于是单调递增栈,现在栈顶元素是小于已弹出的栈顶元素。所以,栈顶元素和当前坐标就是以弹出的栈顶元素的左右两个更低的柱子。那么,就弹出的栈顶元素作为高,当前坐标-栈顶元素(存的坐标)-1作为宽,就可以计算相关柱子组成的面积了。
46+
47+
哨兵技巧非常巧妙。即可减少栈的非空判断,又可以推进剩余元素的计算(如果没有最后的零点哨兵,栈里单调递增的元素最后还要单独处理。)
48+
4449

4550
[[src-0084]]
51+
[tabs]
52+
====
53+
一刷::
54+
+
55+
--
4656
[{java_src_attr}]
4757
----
4858
include::{sourcedir}/_0084_LargestRectangleInHistogram.java[tag=answer]
4959
----
60+
--
61+
62+
// 二刷::
63+
// +
64+
// --
65+
// [{java_src_attr}]
66+
// ----
67+
// include::{sourcedir}/_0084_LargestRectangleInHistogram_2.java[tag=answer]
68+
// ----
69+
// --
70+
====
71+
72+
73+
== 参考资料
5074

75+
. https://leetcode.cn/problems/largest-rectangle-in-histogram/solutions/142012/bao-li-jie-fa-zhan-by-liweiwei1419/[84. 柱状图中最大的矩形 - 暴力解法、栈(单调栈、哨兵技巧)^] -- 哨兵技巧牛逼
76+
. https://leetcode.cn/problems/largest-rectangle-in-histogram/solutions/266844/zhu-zhuang-tu-zhong-zui-da-de-ju-xing-by-leetcode-/[84. 柱状图中最大的矩形 - 官方题解^] -- 看的稀里糊涂。

docs/images/0084-10.png

18.1 KB
Loading

docs/index.adoc

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

251251
include::0083-remove-duplicates-from-sorted-list.adoc[leveloffset=+1]
252252

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

255255
// include::0085-maximal-rectangle.adoc[leveloffset=+1]
256256

logbook/202503.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,12 @@ endif::[]
468468
|{doc_base_url}/0074-search-a-2d-matrix.adoc[题解]
469469
|✅ 二分查找。把矩阵按行“拼接”,然后二分查找,解法非常妙。
470470

471+
|{counter:codes2503}
472+
|{leetcode_base_url}/largest-rectangle-in-histogram/[84. 柱状图中最大的矩形^]
473+
|{doc_base_url}/0084-largest-rectangle-in-histogram.adoc[题解]
474+
|❌ 单调栈。一脸懵逼。哨兵技巧非常巧妙。
475+
476+
471477
|===
472478

473479
截止目前,本轮练习一共完成 {codes2503} 道题。

src/main/java/com/diguage/algo/leetcode/_0083_RemoveDuplicatesFromSortedList.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public class _0083_RemoveDuplicatesFromSortedList {
2222
/**
2323
* Runtime: 1 ms, faster than 22.08% of Java online submissions for Remove Duplicates from Sorted List.
2424
* Memory Usage: 40 MB, less than 7.14% of Java online submissions for Remove Duplicates from Sorted List.
25+
*
26+
* @author D瓜哥 · https://www.diguage.com
27+
* @since 2020-02-04 22:39
2528
*/
2629
public ListNode deleteDuplicates(ListNode head) {
2730
ListNode current = head.next;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
public class _0084_LargestRectangleInHistogram {
7+
// tag::answer[]
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2025-04-23 20:07:47
11+
*/
12+
public int largestRectangleArea(int[] heights) {
13+
int length = heights.length;
14+
if (length == 1) {
15+
return heights[0];
16+
}
17+
int[] newHeights = new int[length + 2];
18+
// 两端是两个哨兵
19+
newHeights[0] = 0;
20+
newHeights[length + 1] = 0;
21+
System.arraycopy(heights, 0, newHeights, 1, length);
22+
heights = newHeights;
23+
int result = 0;
24+
length += 2;
25+
Deque<Integer> stack = new ArrayDeque<>();
26+
stack.push(0); // 哨兵,可以省去栈的非空判断
27+
for (int i = 1; i < length; i++) {
28+
// 单调递增栈
29+
// 当需要出栈的时候,就是当前元素小于栈顶元素。
30+
// 这样,弹出栈顶元素,以弹出的栈顶元素作为高,
31+
// 由于是单调递增栈,现在栈顶元素是小于已弹出的栈顶元素。
32+
// 所以,栈顶元素和当前坐标就是以弹出的栈顶元素的左右两个更低的柱子
33+
// 那么,就弹出的栈顶元素作为高,当前坐标-栈顶元素(存的坐标)-1作为宽
34+
// 就可以计算相关柱子组成的面积了。
35+
while (heights[i] < heights[stack.peekLast()]) {
36+
int currHeight = heights[stack.pollLast()];
37+
int currWidth = i - stack.peekLast() - 1;
38+
result = Math.max(result, currHeight * currWidth);
39+
}
40+
stack.addLast(i);
41+
}
42+
return result;
43+
}
44+
// end::answer[]
45+
public static void main(String[] args) {
46+
new _0084_LargestRectangleInHistogram()
47+
.largestRectangleArea(new int[]{2, 1, 5, 6, 2, 3});
48+
}
49+
}

0 commit comments

Comments
 (0)