LeetCode 84. Largest Rectangle in Histogram
来源:互联网 发布:java 解压war包 编辑:程序博客网 时间:2024/06/04 18:08
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 heights = [2,1,5,6,2,3]
,
return 10
.
求一个最大的矩形面积,之前想用dp思想来解决这道题,发现时间复杂度达到了n2,所以找了一下coder ganker的思想,使用一个栈有小到大存储遍历过的索引。
public class Solution { public int largestRectangleArea(int[] height) { if(height==null || height.length==0) return 0; int max = 0; LinkedList<Integer> stack = new LinkedList<Integer>(); for(int i=0;i<height.length;i++) { while(!stack.isEmpty() && height[i]<=height[stack.peek()]) { int index = stack.pop(); int curArea = stack.isEmpty()?i*height[index]:(i-stack.peek()-1)*height[index]; max = Math.max(max,curArea); } stack.push(i); } while(!stack.isEmpty()) { int index = stack.pop(); int curArea = stack.isEmpty()?height.length*height[index]:(height.length-stack.peek()-1)*height[index]; max = Math.max(max,curArea); } return max; } }
阅读全文
0 0
- [Leetcode] 84. Largest Rectangle in Histogram
- LeetCode --- 84. Largest Rectangle in Histogram
- [LeetCode]*84.Largest Rectangle in Histogram
- [leetcode] 84.Largest Rectangle in Histogram
- [leetcode] 84. Largest Rectangle in Histogram
- Leetcode 84. Largest Rectangle in Histogram
- **LeetCode 84. Largest Rectangle in Histogram
- leetcode 84. Largest Rectangle in Histogram
- leetcode 84. Largest Rectangle in Histogram
- 【leetcode】84. Largest Rectangle in Histogram
- LeetCode 84. Largest Rectangle in Histogram
- LeetCode-84.Largest Rectangle in Histogram
- leetcode.84. Largest Rectangle in Histogram
- [leetcode] 【栈】 84. Largest Rectangle in Histogram
- Leetcode 84. Largest Rectangle in Histogram
- [LeetCode] 84. Largest Rectangle in Histogram
- LeetCode 84. Largest Rectangle in Histogram
- leetcode 84. Largest Rectangle in Histogram
- [ css ]文本溢出显示省略号
- 替换空格、制表符、回车、换行为""
- springmvc mybatis 配置一对多的关系使用 <association> 及实体类写法2Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配自动为该对象填充属性值。支持
- 前端学习(四)node核心模块querystring
- 【我的Android进阶之旅】Jenkins挂载slave节点,增强分布式编译的效率
- LeetCode 84. Largest Rectangle in Histogram
- Knight Moves(BFS模板)
- POJ 3132 Sum of Different Primes 笔记
- Search Insert Position
- 1.1 Avtivity的生命周期全面分析
- [数据库]事务、并发、数据库锁
- 变量的定义位置
- hibernate的简单实现
- 从零搭建Hadoop集群 三