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. C语言每日一题之No.9

    再做决定之前,我还是做好自己该做的.我不希望几年后会悔恨自己为什么在最该努力的时候不愿意吃苦.尊敬的女王陛下,请接题: 一.题目:有已按升序排好顺序的字符串a,编写程序将字符串s中的每个字符按升序的规 ...

  2. Makefile选项CFLAGS,LDFLAGS,LIBS

    CFLAGS 表示用于 C 编译器的选项, CXXFLAGS 表示用于 C++ 编译器的选项.这两个变量实际上涵盖了编译和汇编两个步骤. CFLAGS: 指定头文件(.h文件)的路径,如:CFLAGS ...

  3. Sqlserver 树形查询

    WITH tree AS(SELECT dwpid, dwid,1 AS x2level,dwmc,/*CAST(dwmc AS NVARCHAR(max)) x2name,*/CAST(+dwid ...

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

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

  5. ZooKeeper典型应用场景(转)

    ZooKeeper是一个高可用的分布式数据管理与系统协调框架.基于对Paxos算法的实现,使该框架保证了分布式环境中数据的强一致性,也正是基于这样的特性,使得ZooKeeper解决很多分布式问题.网上 ...

  6. MapReduce编程实现学习

    MapReduce主要包括两个阶段:一个是Map,一个是Reduce. 每一步都有key-value对作为输入和输出. Map阶段的key-value对的格式是由输入的格式决定的,如果是默认的Text ...

  7. 随笔http

    一个HTTP由一条请求命令和一个响应结果组成.这种通信是通过名为HTTP报文(message)的格式化数据块进行的.

  8. BOM(Bill of Material)详解

    一.物料(ITEM) 物料(Item or Material),是对存货的统称,是建立BOM和其他业务数据的前提条件,在ERP系统中称之为物料主数据,包括原材料(Raw material).在产品(W ...

  9. 图片_ _图片缓存之内存缓存技术LruCache,软引用

    每当碰到一些大图片的时候,我们如果不对图片进行处理就会报OOM异常,这个问题曾经让我觉得很烦恼,后来终于得到了解决,那么现在就让我和大家一起分享一下吧.这篇博文要讲的图片缓存机制,我接触到的有两钟,一 ...

  10. android实现 服务器功能

    package com.weijia.tests; import java.io.IOException; import java.net.InetSocketAddress; import java ...