Largest Rectangle in 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.

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.

具体思路也是用stack的思想,相比其他的需要40ms以上我使用了将while循环拆分开来,从而可以将

算法的复杂度更进一步降低,运算时间为16ms

class Solution {
public:
int largestRectangleArea(vector<int>& height) {
if(0==height.size())
return 0;
if(1==height.size())
return height[0];
vector<int>index;
height.push_back(-1);
int count=-1;
int ans=0;
int len=height.size();
for(int i=0;i<height.size();)
{

int peak=index.size()-1;
if(-1==peak)
{
index.push_back(i);
i++;
}
while(i<len&&height[i]>=height[index[peak]])
{
index.push_back(i);
i++;
peak=index.size()-1;
}
peak=index.size()-1;
int tmp=index[peak];
while(peak>-1&&height[index[peak]]>height[i])
{

if(peak==0)
{
count=height[index[peak]]*i;
index.pop_back();
peak=index.size()-1;
}
else
{
int mm=index[peak];
index.pop_back();
peak=index.size()-1;
count=height[mm]*(i-index[peak]-1);
}
if(count>ans)
ans=count;
}
}
return ans;
}
};

【LeetCode】84. Largest Rectangle in Histogram的更多相关文章

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

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

  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 85. Maximal Rectangle

    问题描述: 84:直方图最大面积. 85:0,1矩阵最大全1子矩阵面积. 问题分析: 对于84,如果高度递增的话,那么OK没有问题,不断添加到栈里,最后一起算面积(当然,面积等于高度h * disPo ...

  4. 【一天一道LeetCode】#84. Largest Rectangle in Histogram

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...

  5. 【LeetCode】084. Largest Rectangle in Histogram

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

  6. LeetCode OJ 84. Largest Rectangle in Histogram

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

  7. 【Lintcode】122.Largest Rectangle in Histogram

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

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

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

  9. 【LeetCode】85. Maximal Rectangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...

随机推荐

  1. 在Huawei USG2100 上配置通过Huawei VPN客户端的接入

    USG2100 设置 一.本地策略 中允许 Untrust 对 L2TP 的访问: 二.勾选 VPN-->L2TP 启用: 三.设置参数: 1.组类型选择LNS,本端隧道名称LNS,对端隧道名称 ...

  2. C#中abstract和virtual区别

    在C#的学习中,容易混淆virtual方法和abstract方法的使用,现在来讨论一下二者的区别.二者都牵涉到在派生类中与override的配合使用. 一.Virtual方法(虚方法) virtual ...

  3. 黄聪:TinyMCE 4 增强 添加样式、按钮、字体、下拉菜单和弹出式窗口

    我最喜欢 WordPress 3.9 的更新是使用了 TinyMCE 4.0 编辑器.新的 TinyMCE 看起来看起来更整洁(真正匹配WP仪表板),它有一些非常不错的附加功能.我的很多老主题和插件必 ...

  4. application in 2014

    OA WEBSITE ERP ISO ANDROID EF+MVC4+CATCHE+JQuery+js+div+css+web性能优化+webservice+sql2008+设计模式+wcf+多线程 ...

  5. Ant -- Another Neat Tool

      最早用来构建著名的Tomcat,可以看成是一个Java版本的Make.也正因为使用了Java,Ant是跨平台的.   Ant有一个构建脚本build.xml <?xml version = ...

  6. Spring配置xml文件详解

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  7. PLSQL_基础系列02_分组函数GROUP BY / ROLLUP / CUBE(案例)

    2014-11-30 Created By BaoXinjian

  8. imageView--转-如何代码动态的获取和设置ImageView的宽度和高度?

    11- 12- 13-android获取图片原始长宽; ========= 11--国内某金融企业的一道面试题:如何在代码中获得一个ImageVIew的高度和宽度? 相信很大一部分人,会直接想到用im ...

  9. struts2请求过程源码分析

    Struts2是Struts社区和WebWork社区的共同成果,我们甚至可以说,Struts2是WebWork的升级版,他采用的正是WebWork的核心,所以,Struts2并不是一个不成熟的产品,相 ...

  10. jquery实现点击页面其他地方隐藏指定元素

    代码实例如下: <!DOCTYPE html><html><head><meta charset=" utf-8"><meta ...