[leetcode]84. Largest Rectangle in Histogram(Java)
来源:互联网 发布:知乎哲理段落 编辑:程序博客网 时间:2024/06/03 16:21
https://leetcode.com/problems/largest-rectangle-in-histogram/#/description
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
.
package go.jacob.day702;import java.util.Stack;public class Demo2 {/* * Runtime: 24 ms.Your runtime beats 67.12 % of java submissions. */public int largestRectangleArea(int[] heights) {if (heights == null || heights.length < 1)return 0;Stack<Integer> stack = new Stack<Integer>();stack.push(-1);int max = 0;for (int i = 0; i < heights.length; i++) {// 把堆栈中比heights[i]大的元素都pop出,并计算面积while (stack.peek() != -1 && heights[i] < heights[stack.peek()]) {int top = stack.pop();// 面积计算公式:(i - 1-stack.peek() ) * heights[top])max = Math.max(max, (i - 1 - stack.peek()) * heights[top]);}stack.push(i);}while (stack.peek() != -1) {int top = stack.pop();max = Math.max(max, (heights.length - 1 - stack.peek()) * heights[top]);}return max;}}
阅读全文
0 0
- [leetcode]84. Largest Rectangle in Histogram(Java)
- [Leetcode] Largest Rectangle in Histogram (Java)
- [LeetCode][Java] Largest Rectangle in Histogram
- Largest Rectangle in Histogram leetcode java
- [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
- PAT乙级 1058. 选择题(20)
- 数据库表的复杂操作
- ArcGIS API For JavaScript——空间查询(FindTask和IdentifyTask)
- springMVC后台校验实现
- 插入排序
- [leetcode]84. Largest Rectangle in Histogram(Java)
- Python 爬虫入门 2 Phantomjs+Python
- DirectX11(四)
- pppyhon tushare笔记(四)-股票分类数据
- Log4j学习汇总—–配置详解
- iOS开发中识别图中的二维码
- 【Spring Cloud学习】服务消费者
- 在Predix上运行Scala的HelloWorld
- 理解依赖注入和控制反转