Skip to content

Commit 51f5118

Browse files
committed
Add solution 084
1 parent 0a3b402 commit 51f5118

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Complete solutions to Leetcode problems, updated daily.
6868
|---|---|---|
6969
| 023 | [Merge k Sorted Lists](https://github.com/doocs/leetcode/tree/master/solution/023.Merge%20k%20Sorted%20Lists) | `Linked List`, `Divide and Conquer`, `Heap` |
7070
| 032 | [Longest Valid Parentheses](https://github.com/doocs/leetcode/tree/master/solution/032.Longest%20Valid%20Parentheses) | `String`, `Dynamic Programming` |
71+
| 084 | [Largest Rectangle in Histogram](https://github.com/doocs/leetcode/tree/master/solution/084.Largest%20Rectangle%20in%20Histogram) | `Array`, `Stack` |
7172
| 145 | [Binary Tree Postorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/145.Binary%20Tree%20Postorder%20Traversal) | `Stack`, `Tree` |
7273

7374
## Contributions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
## 柱状图中最大的矩形
2+
### 题目描述
3+
4+
给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
5+
6+
求在该柱状图中,能够勾勒出来的矩形的最大面积。
7+
8+
![histogram](http://p9ucdlghd.bkt.clouddn.com/histogram.png)
9+
10+
以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为 `[2,1,5,6,2,3]`
11+
12+
图中阴影部分为所能勾勒出的最大矩形面积,其面积为 `10` 个单位。
13+
14+
![histogram_area](http://p9ucdlghd.bkt.clouddn.com/histogram_area.png)
15+
16+
示例:
17+
```
18+
输入: [2,1,5,6,2,3]
19+
输出: 10
20+
```
21+
22+
### 解法
23+
从前往后遍历 heightss[0...n]
24+
25+
- 若 heightss[i] > heightss[i - 1],则将 i 压入栈中;
26+
- 若 heightss[i] <= heightss[i - 1],则依次弹出栈,计算栈中能得到的最大矩形面积。
27+
28+
注意,压入栈中的是柱子的索引,而非柱子的高度。(通过索引可以获得高度、距离差)
29+
30+
```java
31+
class Solution {
32+
public int largestRectangleArea(int[] heights) {
33+
if (heights == null || heights.length == 0) {
34+
return 0;
35+
}
36+
37+
int n = heights.length;
38+
if (n == 1) {
39+
return heights[0];
40+
}
41+
42+
// 创建一个新的数组,数组长度为 n + 1,最后一个元素值赋为 0
43+
// 确保在后面的遍历中,原数组最后一个元素值能得到计算
44+
int[] heightss = new int[n + 1];
45+
heightss[n] = 0;
46+
for (int i = 0; i < n; ++i) {
47+
heightss[i] = heights[i];
48+
}
49+
50+
Stack<Integer> stack = new Stack<>();
51+
int max = 0;
52+
53+
for (int i = 0; i <= n;) {
54+
if (stack.isEmpty() || heightss[i] > heightss[stack.peek()]) {
55+
stack.push(i++);
56+
} else {
57+
int index = stack.pop();
58+
max = Math.max(max, heightss[index] * (stack.isEmpty() ? i : i - stack.peek() - 1));
59+
}
60+
}
61+
62+
return max;
63+
64+
65+
}
66+
}
67+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public int largestRectangleArea(int[] heights) {
3+
if (heights == null || heights.length == 0) {
4+
return 0;
5+
}
6+
7+
int n = heights.length;
8+
if (n == 1) {
9+
return heights[0];
10+
}
11+
12+
// 创建一个新的数组,数组长度为 n + 1,最后一个元素值赋为 0
13+
// 确保在后面的遍历中,原数组最后一个元素值能得到计算
14+
int[] heightss = new int[n + 1];
15+
heightss[n] = 0;
16+
for (int i = 0; i < n; ++i) {
17+
heightss[i] = heights[i];
18+
}
19+
20+
Stack<Integer> stack = new Stack<>();
21+
int max = 0;
22+
23+
for (int i = 0; i <= n;) {
24+
if (stack.isEmpty() || heightss[i] > heightss[stack.peek()]) {
25+
stack.push(i++);
26+
} else {
27+
int index = stack.pop();
28+
max = Math.max(max, heightss[index] * (stack.isEmpty() ? i : i - stack.peek() - 1));
29+
}
30+
}
31+
32+
return max;
33+
34+
}
35+
}

0 commit comments

Comments
 (0)