Leetcode 84. Largest Rectangle in Histogram
2016-01-24 11:31
211 查看
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.
![](http://www.leetcode.com/wp-content/uploads/2012/04/histogram.png)
Above is a histogram where width of each bar is 1, given height =
![](http://www.leetcode.com/wp-content/uploads/2012/04/histogram_area.png)
The largest rectangle is shown in the shaded area, which has area =
For example,
Given heights =
return
Analysis: The trick is to use the stack to record the position. The idea is from here
http://www.programcreek.com/2014/05/leetcode-largest-rectangle-in-histogram-java/
ublic int largestRectangleArea(int[] height) {
if (height == null || height.length == 0) {
return 0;
}
Stack<Integer> stack = new Stack<Integer>();
int max = 0;
int i = 0;
while (i < height.length) {
//push index to stack when the current height is larger than the previous one
if (stack.isEmpty() || height[i] >= height[stack.peek()]) {
stack.push(i);
i++;
} else {
//calculate max value when the current height is less than the previous one
int p = stack.pop();
int h = height[p];
int w = stack.isEmpty() ? i : i - stack.peek() - 1;
max = Math.max(h * w, max);
}
}
while (!stack.isEmpty()) {
int p = stack.pop();
int h = height[p];
int w = stack.isEmpty() ? i : i - stack.peek() - 1;
max = Math.max(h * w, max);
}
return max;
}
![](http://www.leetcode.com/wp-content/uploads/2012/04/histogram.png)
Above is a histogram where width of each bar is 1, given height =
[2,1,5,6,2,3].
![](http://www.leetcode.com/wp-content/uploads/2012/04/histogram_area.png)
The largest rectangle is shown in the shaded area, which has area =
10unit.
For example,
Given heights =
[2,1,5,6,2,3],
return
10.
Analysis: The trick is to use the stack to record the position. The idea is from here
http://www.programcreek.com/2014/05/leetcode-largest-rectangle-in-histogram-java/
ublic int largestRectangleArea(int[] height) {
if (height == null || height.length == 0) {
return 0;
}
Stack<Integer> stack = new Stack<Integer>();
int max = 0;
int i = 0;
while (i < height.length) {
//push index to stack when the current height is larger than the previous one
if (stack.isEmpty() || height[i] >= height[stack.peek()]) {
stack.push(i);
i++;
} else {
//calculate max value when the current height is less than the previous one
int p = stack.pop();
int h = height[p];
int w = stack.isEmpty() ? i : i - stack.peek() - 1;
max = Math.max(h * w, max);
}
}
while (!stack.isEmpty()) {
int p = stack.pop();
int h = height[p];
int w = stack.isEmpty() ? i : i - stack.peek() - 1;
max = Math.max(h * w, max);
}
return max;
}
相关文章推荐
- LINQ↔Lambda↔SQL 互相“翻译”
- HTML5一些元素的整理
- NIO缓存区(4)之通道、管道
- Android基础之AlertDialog(对话框)的学习
- android:inputType参数类型说明
- MiZ702学习笔记12——封装一个普通的VGA IP
- 已知后序中序序列求先序序列
- NIO缓冲区(3)之分散(Scatter)/聚集(Gather)、通道之间的数据传输和选择器(Selector)
- 友情序言 to 《Learning Spark: Lightning-fast big data analytics》
- Android系统更改状态栏字体颜色
- Castle IOC容器与Spring.NET配置之比较
- grid
- MFC---获得组合框CComboBox中选中的内容
- ios关于retainCount的一些疑问
- 联创世纪集团2016年落地师专业培训圆满落幕
- POJ 2516 Minimum Cost (最小费用最大流)
- React-Native个人信息界面
- kafka-zookepeper
- JAVA——概述
- 使用python发送邮件