【leetcode】Array——Largest Rectangle in Histogram(84)
2016-03-13 13:26
381 查看
题目:
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.
For example,
Given heights =
return
思路:Stack
见链接:http://www.geeksforgeeks.org/largest-rectangle-under-histogram/
代码:
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.
For example,
Given heights =
[2,1,5,6,2,3],
return
10.
思路:Stack
见链接:http://www.geeksforgeeks.org/largest-rectangle-under-histogram/
代码:
public class Solution { public int largestRectangleArea(int[] height) { int len = height.length; Stack<Integer> s = new Stack<Integer>(); int maxArea = 0; for(int i = 0; i <= len; i++){ int h = (i == len ? 0 : height[i]); if(s.isEmpty() || h >= height[s.peek()]){ s.push(i); }else{ int tp = s.pop(); maxArea = Math.max(maxArea, height[tp] * (s.isEmpty() ? i : i - 1 - s.peek())); i--; } } return maxArea; } }
相关文章推荐
- 变量
- 分数化小数
- ProGuard Eclipse Android
- 剑指offer-二叉搜索树与双向链表
- 第三周项目3:输出星号图
- javascript常识
- hdoj 欧拉回路 1878 (并查集)
- This Android SDK requires Android Developer Toolkit version 23.0.0 or above
- 如何创建指定大小的数组/字符串
- 技术路线的思考
- 单元测试——四则运算2
- Hadoop完全分布式集群配置
- Hadoop完全分布式集群配置
- 《linux内核与分析》第三周
- hdoj--1877--又一版 A+B(水题)
- Javascript中各种高度宽度解读
- MBR反汇编
- OJ提交题目中的语言选项里G++与C++的区别
- Vmware虚拟机下三种网络模式配置
- Java 数组实现冒泡排序