leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法
来源:互联网 发布:数据增值服务内容 编辑:程序博客网 时间:2024/05/21 06:14
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
.
代码如下:
public class Solution { public int largestRectangleArea(int[] height) { int max = 0;//最大值 int i = 0;//左指针 int j = height.length - 1;//右指针 boolean isMinChange = true; //双指针扫描 while(i <= j){ int minHeight = Integer.MAX_VALUE;//范围内最小值 if(isMinChange){//最小值是否改变 isMinChange = false; //重新得到最小值 for(int k = i ; k <= j;k++){ if(height[k] < minHeight){ minHeight = height[k]; } } } //面积 int area = (j - i + 1)*minHeight; if(max < area){ max = area; } if(i == j){//如果相等,则结束 break; } if(height[i] < height[j]){//左指针增加,直到比当前大 int k = i; while(k <= j && height[k] <= height[i]){ if(height[k] == minHeight){//判断最小值是否改变 isMinChange = true; } k++; } i = k; }else{//右指针减小,直到比当前大 int k = j; while( k >= i && height[k] <= height[j]){ if(height[k] == minHeight){//判断最小值是否改变 isMinChange = true; } k--; } j = k; } } return max; }}
解法上过不了OJ,所以只能在网上参看资料,最后找到资料如下,是用栈解决的。
public class Solution { public int largestRectangleArea(int[] height) { if (height == null || height.length == 0) return 0; Stack<Integer> stHeight = new Stack<Integer>(); Stack<Integer> stIndex = new Stack<Integer>(); int max = 0; for(int i = 0; i < height.length; i++){ if(stHeight.isEmpty() || height[i] > stHeight.peek()){ stHeight.push(height[i]); stIndex.push(i); }else if(height[i] < stHeight.peek()){ int lastIndex = 0; while(!stHeight.isEmpty() && height[i] < stHeight.peek()){ lastIndex = stIndex.pop(); int area = stHeight.pop()*(i - lastIndex); if(max < area){ max = area; } } stHeight.push(height[i]); stIndex.push(lastIndex); } } while(!stHeight.isEmpty()){ int area = stHeight.pop()*(height.length - stIndex.pop()); max = max < area ? area:max; }return max; }}
0 0
- leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法
- LeetCode 84. Largest Rectangle in Histogram(直方图最大矩形)
- Leetcode 84. Largest Rectangle in Histogram 最大矩形 解题报告
- [Leetcode] largest rectangle in histogram 直方图中最大的矩形
- LeetCode 84. Largest Rectangle in Histogram(最大矩形)
- LeetCode | Largest Rectangle in Histogram(直方图围城的最大矩形面积)
- LeetCode OJ 之 Largest Rectangle in Histogram (直方图中的最大矩形)
- [LeetCode] Largest Rectangle in Histogram 解题思路
- leetcode 84. Largest Rectangle in Histogram 最大直方图
- Largest Rectangle in Histogram (直方图中最大面积) 【leetcode】
- 直方图最大矩形问题(Largest Rectangle in Histogram)
- 直方图最大矩形问题(Largest Rectangle in Histogram)
- Largest Rectangle in Histogram 直方图中最大的矩形面积
- 寻找直方图中的最大矩形 Largest Rectangle in Histogram
- Largest Rectangle in Histogram 直方图中最大的矩形
- [LeetCode]—Largest Rectangle in Histogram 求直方图最大填充矩形面积
- LeetCode: Largest Rectangle in Histogram(直方图最大面积)
- 4.1.3LeetCode: Largest Rectangle in Histogram(直方图最大面积)
- 图片折叠效果:Layer的contentsRect属性和渐变层
- EXITTHRD.C--示范ExitThread()
- oracle 数据汞导入导出dmp文件
- AutoLayout
- 百度推送之Im
- leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法
- Linux Shell编程学习笔记(2)
- linux的共享中断机制
- 互联网+农业电商
- Redhat7/Centos7 安装配置 RabbitMQ
- Flume环境部署和配置详解及案例大全
- Ubuntu 15.04 root登录,无声音解决方案
- Win7下实现 lpk.dll劫持游戏注入
- hdu1372Knight Moves