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.

思路:此题还是有一些难度的,刚開始想的双指针。前后扫描,可是每次求最小的高度的时候都须要遍历一次,效率上不是非常高。为o(n2)。

代码例如以下:

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;
}
}

leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法的更多相关文章

  1. LeetCode 84. Largest Rectangle in Histogram 单调栈应用

    LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...

  2. [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  3. [leetcode]84. Largest Rectangle in Histogram直方图中的最大矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  4. [leetcode]84.Largest Rectangle in Histogram ,O(n)解法剖析

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  5. [LeetCode#84]Largest Rectangle in Histogram

    Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...

  6. LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形

    原题 Given n non-negative integers representing the histogram's bar height where the width of each bar ...

  7. 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...

  8. 84. Largest Rectangle in Histogram

    https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...

  9. 刷题84. Largest Rectangle in Histogram

    一.题目说明 题目84. Largest Rectangle in Histogram,给定n个非负整数(每个柱子宽度为1)形成柱状图,求该图的最大面积.题目难度是Hard! 二.我的解答 这是一个 ...

随机推荐

  1. Bzoj3227 [Sdoi2008]红黑树(tree)

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 204  Solved: 125 Description 红黑树是一类特殊的二叉搜索树,其中每个结点被染 ...

  2. 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest A E F G H I K M

    // 深夜补水题,清早(雾)写水文 A. Automatic Door 题意 \(n(n\leq 1e9)\)个\(employee\)和\(m(m\leq 1e5)\)个\(client\)要进门, ...

  3. 【Visual Studio】Visual Studio 2010 "LNK1123: 转换到 COFF 期间失败: 文件无效或损坏" 的解决方法

    1.将 项目|项目属性|配置属性|连接器|清单文件|嵌入清单 “是”改为“否”. 2.找到 C:\Windows\winsxs\x86_netfx-cvtres_for_vc_and_vb_b03f5 ...

  4. webpack 2.6.1配置笔记

    2017-09-11更新:更新到webpack 2.6.1所对应的配置,完善部分代码注释. 由于最近在vue-cli生成的webpack模板项目的基础上写一个小东西,开发过程中需要改动到build和c ...

  5. (7)JavaScript-BOM(浏览器对象模 型)

    window 对象 全局作用域 在浏览器中, window 对象有双重角色,它既是通过 JavaScript 访问浏览器窗口的一个接口,又是 ECMAScript 规定的 Global 对象. 所有在 ...

  6. apache2.4+tomcat8+jk1.2.40集群配置

    由于目前很多apache+tomcat集群都是在apache2.2上配置的,Apache2.4的教程几乎没有,这里写一篇记录下来. 环境:apache2.4.12(Apache Haus编译版本).t ...

  7. 可靠UDP设计

    最近加入了一个用帧同步的项目,帧同步方案对网络有着极大的影响,于是采用了RUDP(可靠UDP),那么为什么要摒弃TCP,而费尽心思去采用UDP呢?要搞明白这个问题,首先要了解TCP和UDP的区别 , ...

  8. POJ 2406 Power Strings KMP算法之next数组的应用

    题意:给一个字符串,求该串最多由多少个相同的子串相接而成. 思路:只要做过poj 1961之后,这道题就很简单了.poj 1961 详细题解传送门. 假设字符串的长度为len,如果 len % (le ...

  9. SecureCRT介绍、安装、使用(转)

    http://blog.csdn.net/liang19890820/article/details/49701429 简介 SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简 ...

  10. UIView的任意圆角

    今天在做项目的时候,遇到一个问题,grouped类型的tableview 怎么样才能让他们的一个view 其中一个角圆角? 如上图所示,其实我是用UILabel,但是箭头的位置总是尖的不太好看.设置l ...