LeetCode84 --Largest Rectangle in Histogram
来源:互联网 发布:淘宝买东西物流不更新 编辑:程序博客网 时间:2024/06/05 09:27
Largest Rectangle in Histogram
Question:
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.
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]
.
The largest rectangle is shown in the shaded area, which has area = 10
unit.
For example,
Given height = [2,1,5,6,2,3]
,
return 10
.
解法一
使用动态规划,用left[i]表示第i个柱子可以最多向左延伸至第left[i]个柱子,形成一个矩形,right[i]则表示向右延伸。遍历两次,分别计算出这两个数组。
再遍历一次,即可求出所有的柱子可以形成的最大的矩形面积。为了减少边界的判断,可以使用哨兵,在两端添加两个柱子高度都为-1.
01
//============================================================================
02
// Name : MaxRectagle.java
03
// Author : GaoTong
04
// Date : 2014/9/5
05
// Copyright : www.acmerblog.com
06
//============================================================================
07
public
class
MaxRectagle {
08
public
static
void
main(String args[]){
09
int
height[] = {
2
,
1
,
5
,
6
,
2
,
3
};
10
int
ans = getMaxRectangle(height);
11
System.out.println(ans);
12
}
13
14
public
static
int
getMaxRectangle (
int
heights[]){
15
int
ans =
0
;
16
int
n = heights.length;
17
int
left[] =
new
int
[n+
1
];
18
int
right[] =
new
int
[n+
1
];
19
processLR(heights, left, right);
20
for
(
int
i=
1
; i<=n; i++){
21
int
tmp = (right[i]-left[i]+
1
) * heights[i-
1
];
22
if
( ans < tmp)
23
ans = tmp;
24
}
25
return
ans;
26
}
27
28
public
static
void
processLR(
int
heights[],
int
left[],
int
right[]){
29
int
n = heights.length;
30
//用临时数组,设置两个哨兵
31
int
tempArr[] =
new
int
[n+
2
];
32
tempArr[
0
] = -
1
;
33
for
(
int
i=
1
; i<=n; i++) tempArr[i] = heights[i-
1
];
34
tempArr[tempArr.length-
1
] = -
1
;
35
36
for
(
int
i=
1
; i<=n; i++){
37
int
k = i;
38
while
( tempArr[i] <= tempArr[k-
1
])
39
k = left[k-
1
];
40
left[i] = k;
41
}
42
43
for
(
int
i=n; i>
0
; i--){
44
int
k = i;
45
while
( tempArr[i] <= tempArr[k+
1
])
46
k = right[k+
1
];
47
right[i] = k;
48
}
49
}
50
}
算法的时间复杂度为 O(n)。 再求left[]和right[]时,虽然里面有while循环,但可以保证复杂度为O(n)
Analysis:
When solving this problem, the straightforward approach is to consider all possible cases, i.e., we begin with the ith rectangle, and then enumerate the remaining rectangles after the ith rectangle, each time, we need to compare the area with the maximum area. The time complexity is O(n^2).
Actually, we can decrease the complexity by using stack to keep track of the height and start indexes. Compare the current height with previous one.
Case 1: current > previous (top of height stack)
Push current height and index as candidate rectangle start position.
Case 2: current = previous
Ignore.
Case 3: current < previous
Need keep popping out previous heights, and compute the candidate rectangle with height and width (current index - previous index). Push the height and index to stacks.
(Note: it is better use another different example to walk through the steps, and you will understand it better).
如果高度一致都是递增的就一直压入栈,一旦遇到一个高度减小的,就计算栈里面能够组成的最大四边形面积(一个个出栈分别计算四边形面积)
代码:
/*public class Solution {
public int largestRectangleArea(int[] heights) {
if (height == null || height.length == 0) {
return 0;
}
Stack<Integer> stack = new Stack<Integer>();
int max = 0;
for (int i = 0; i <= height.length; i++) {
int curt = (i == height.length) ? -1 : height[i];
while (!stack.isEmpty() && curt <= height[stack.peek()]) {
int h = height[stack.pop()];
int w = stack.isEmpty() ? i : i - stack.peek() - 1;
max = Math.max(max, h * w);
}
stack.push(i);
}
return max;
}
}
*/
- leetcode84: Largest Rectangle in Histogram
- LeetCode84. Largest Rectangle in Histogram
- LeetCode84. Largest Rectangle in Histogram
- LeetCode84 --Largest Rectangle in Histogram
- leetcode84. Largest Rectangle in Histogram
- Leetcode84. Largest Rectangle in Histogram
- Leetcode84. Largest Rectangle in Histogram.
- LeetCode84 Largest Rectangle in Histogram
- LeetCode84 Largest Rectangle in Histogram java题解
- LeetCode84——Largest Rectangle in Histogram
- 贪心2-- Largest Rectangle in Histogram(LeetCode84)
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- 七个最重要的职业建议--阮一峰
- VC++运算符与优先级
- makefile 和shell文件相互调用
- 谁让APP工程师产生了泡沫?
- 在git@osc上有两个码云账号,如何配置互不干扰?
- LeetCode84 --Largest Rectangle in Histogram
- contentSize、contentOffset、contentInset
- 网络编程中的UDP通信
- loadrunner关联技术的一个示例
- Path expected for join!错误处理
- appstore审核的那些事 --1. 卡死在:正在通过 iTunes Store进行鉴定
- 用dbschema工具导出Informix数据库的结构的具体步骤:
- 结构体/枚举/共用体
- SVN:cleanup failed to process the following paths 错误的解决方案