题目地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ ,刚开始其实没做这个题,而是在做https://oj.leetcode.com/problems/maximal-rectangle/其中很重要的一步就是用到Largest Rectangular Area in a 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.

参考这篇博文思想:http://www.geeksforgeeks.org/largest-rectangle-under-histogram/

For example, consider the following histogram with 7 bars of heights {6, 2, 5, 4, 5, 2, 6}. The largest possible rectangle possible is 12 (see the below figure, the max area rectangle is highlighted in red)

算法思想:

For every bar ‘x’, we calculate the area with ‘x’ as the smallest bar in the rectangle. If we calculate such area for every bar ‘x’ and find the maximum of all areas, our task is done. How to calculate area with ‘x’ as smallest bar? We need to know index of the first smaller (smaller than ‘x’) bar on left of ‘x’ and index of first smaller bar on right of ‘x’. Let us call these indexes as ‘left index’ and ‘right index’ respectively.
We traverse all bars from left to right, maintain a stack of bars. Every bar is pushed to stack once. A bar is popped from stack when a bar of smaller height is seen. When a bar is popped, we calculate the area with the popped bar as smallest bar. How do we get left and right indexes of the popped bar – the current index tells us the ‘right index’ and index of previous item in stack is the ‘left index’.

算法流程:

1) Create an empty stack.

2) Start from first bar, and do following for every bar ‘hist[i]‘ where ‘i’ varies from 0 to n-1.
……a) If stack is empty or hist[i] is higher than the bar at top of stack, then push ‘i’ to stack.
……b) If this bar is smaller than the top of stack, then keep removing the top of stack while top of the stack is greater. Let the removed bar be hist[tp]. Calculate area of rectangle with hist[tp] as smallest bar. For hist[tp], the ‘left index’ is previous (previous to tp) item in stack and ‘right index’ is ‘i’ (current index).

3) If the stack is not empty, then one by one remove all bars from stack and do step 2.b for every removed bar.

代码java实现

public class Solution {
public int largestRectangleArea(int[] height) {
int maxarea = 0;
Stack<Integer> sta = new Stack<>();
int top ;
int top_area;
int i = 0;
while(i<height.length){
if(sta.isEmpty() || height[sta.peek()]<=height[i] ){
sta.push(i++);
}else{
top = sta.pop();
top_area = height[top] * (sta.isEmpty()? i:i-sta.peek()-1);
if(top_area>maxarea){
maxarea = top_area;
}
}
}
while(!sta.isEmpty()){
top = sta.pop();
top_area = height[top] * (sta.isEmpty()? i:i-sta.peek()-1);
if(top_area>maxarea){
maxarea = top_area;
}
}
return maxarea;
}
}

时间复杂度:因为每个元素只push pop一次,时间复杂度O(n).

Largest Rectangular Area in a Histogram相关推荐

  1. 【二分+二维前缀和】Largest Allowed Area

    Largest Allowed Area 时间限制: 1 Sec  内存限制: 128 MB 提交: 146  解决: 54 [提交] [状态] [命题人:admin] 题目描述 A company ...

  2. Leetcode812.Largest Triangle Area最大三角形面积

    给定包含多个点的集合,从其中取三个点组成三角形,返回能组成的最大三角形的面积. 示例: 输入: points = [[0,0],[0,1],[1,0],[0,2],[2,0]] 输出: 2 解释: 这 ...

  3. Gym 102091L Largest Allowed Area 【二分+二维前缀和】

    <题目链接> 题目大意: 给你一个由01组成的矩形,现在问你,该矩形中,最多只含一个1的正方形的边长最长是多少. 解题分析: 用二维前缀和维护一下矩形的01值,便于后面直接$O(1)$查询 ...

  4. HDU 1506 Largest Rectangle in a Histogram(dp、单调栈)

    你是不是飘了?骚年! Problem Description A histogram is a polygon composed of a sequence of rectangles aligned ...

  5. Largest Rectangle in a Histogram (动态规划+奇思妙想单调栈)求最大矩状图面积

    感觉动态规划都是玄妙的很,思维题吧(单调栈思维) 题解:让求最大矩形面积,宽为1,暴力超时 可以发现   当第i-1个比第i个高的时候   比第i-1个高的所有也一定比第i个高 于是可以用到动态规划的 ...

  6. *【HDU - 1506】【POJ - 2559】Largest Rectangle in a Histogram(单调栈或动态规划)

    题干: Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  7. Largest Rectangle in a Histogram HDU - 1506 解题思路 单调栈

    原题目 Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a c ...

  8. hdu 1506 Largest Rectangle in a Histogram 最大矩形

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1506 Largest Rectangle in a Histogram Time Limit: 20 ...

  9. 机器人学习--Hans Moravec在斯坦福博士论文1980年-Obstacle Avoidance and Navigation in the Real World by a Seeing Ro

    Hans Moravec,占用栅格地图的发明人. Obstacle Avoidance and Navigation in the Real World by a Seeing Robot Rover ...

最新文章

  1. 什么是高层主管支持系统?
  2. 修改mysql字符集_mysql 修改字符集
  3. Git 怎么创建本地库,向本地库提交文件
  4. How to enable multiple text type for Product
  5. 在opencv中实现中文输出
  6. Xcode启动RN报错“`fsevents` unavailable“
  7. 定制Android系统开发之二——系统服务
  8. 遗传算法和禁忌搜索解TSP
  9. html实现图片裁剪,JavaScript html js图片切割系统,裁剪,图片处理
  10. 让 snoop 支持 .NET Core WPF 调试
  11. 《MIT科技评论》“35位35岁以下科技创新青年”名单出炉!AI领域有5人入选 | 2020中国区...
  12. A-priori算法
  13. 华为云上云迁移工具案例实践:阿里云迁移到华为云
  14. CTU Open Contest 2019 F. Beer Marathon(贪心)
  15. 论文学习笔记:Detecting and quantifying causal associations in large nonlinear time series datasets
  16. 光伏电站清扫机器人_光伏电站清扫机器人_雷曼科林
  17. ROS学习之工作空间与创建过程
  18. 用tikz画球坐标系下的体积微元
  19. 面向智能网联汽车边缘网络的分布式端-边协同算法
  20. 永远不能懈怠,要记住,黎明之前,最为黑暗

热门文章

  1. input默认值,点击清除,离开恢复
  2. python3.9不支持pyhanlp
  3. c语言字符串malloc,C语言 复制字符串 malloc
  4. 在王者荣耀角度下分析面向对象程序设计B中23种设计模式之装饰模式
  5. unity3d 获取cpu 型号 android
  6. 15-Ansible常用模块-cron模块
  7. 安卓最新系统_太吝啬?一加只给两款手机升级安卓最新系统
  8. android7.0 电源(Power)键流程
  9. ride传递参数是unicode方式解决方法
  10. minio 授予永久访问权限_没有授权,Android App 也能获取你的权限?!