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.

首先想到一种最直观的方法:把平面看做大矩阵,条形看做1,非条形看做0,寻找最大全1矩阵。

思路上是正确的,可以用DP来做,不过会超时。

网上看到一种借助栈的做法,代码很漂亮,但是解释都非常模糊,我看懂之后,决定仔细描述思路如下:

1、如果已知height数组是升序的,应该怎么做?

比如1,2,5,7,8

那么就是(1*5) vs. (2*4) vs. (5*3) vs. (7*2) vs. (8*1)

也就是max(height[i]*(size-i))

2、使用栈的目的就是构造这样的升序序列,按照以上方法求解。

但是height本身不一定是升序的,应该怎样构建栈?

比如2,1,5,6,2,3

(1)2进栈。s={2}, result = 0

(2)1比2小,不满足升序条件,因此将2弹出,并记录当前结果为2*1=2。

将2替换为1重新进栈。s={1,1}, result = 2

(3)5比1大,满足升序条件,进栈。s={1,1,5},result = 2

(4)6比5大,满足升序条件,进栈。s={1,1,5,6},result = 2

(5)2比6小,不满足升序条件,因此将6弹出,并记录当前结果为6*1=6。s={1,1,5},result = 6

2比5小,不满足升序条件,因此将5弹出,并记录当前结果为5*2=10(因为已经弹出的5,6是升序的)。s={1,1},result = 10

2比1大,将弹出的5,6替换为2重新进栈。s={1,1,2,2,2},result = 10

(6)3比2大,满足升序条件,进栈。s={1,1,2,2,2,3},result = 10

栈构建完成,满足升序条件,因此按照升序处理办法得到上述的max(height[i]*(size-i))=max{3*1, 2*2, 2*3, 2*4, 1*5, 1*6}=8<10

综上所述,result=10

class Solution {
public:
int largestRectangleArea(vector<int> &height) {
int ret = 0;
stack<int> stk;
for(int i = 0; i < height.size(); i ++)
{
if(stk.empty() || stk.top() <= height[i])
stk.push(height[i]);
else
{
int count = 0;
while(!stk.empty() && stk.top() > height[i])
{
count ++;
ret = max(ret, stk.top()*count);
stk.pop();
}
while(count --)
stk.push(height[i]);
stk.push(height[i]);
}
}
int count = 1;
while(!stk.empty())
{
ret = max(ret, stk.top()*count);
stk.pop();
count ++;
}
return ret;
}
};

(数组)Largest Rectangle in Histogram(栈解问题)的更多相关文章

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

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

  2. leetcode Largest Rectangle in Histogram 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...

  3. leetcode之Largest Rectangle in Histogram

    问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...

  4. LeetCode 笔记系列 17 Largest Rectangle in Histogram

    题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...

  5. LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram

    1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...

  6. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

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

    题目描述 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的 ...

  8. Largest Rectangle in Histogram及二维解法

    昨天看岛娘直播解题,看到很经典的一题Largest Rectangle in Histogram 题目地址:https://leetcode.com/problems/largest-rectangl ...

  9. 47. Largest Rectangle in Histogram && Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

随机推荐

  1. Linux命令学习(21):netstat命令

    版权声明 更新:2017-06-13博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的netstat ...

  2. java-10异常处理动手动脑

    1.请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识. import javax.swing.*; class AboutExcep ...

  3. uoj#87. mx的仙人掌

    //Achen #include<bits/stdc++.h> #define For(i,a,b) for(int i=(a);i<=(b);i++) #define Rep(i, ...

  4. hw_module_t 加载过程

    每一个HAL模块都有一个ID值,以这些ID值为参数来调用硬件抽象层提供的函数hw_get_module就可以将指定的模块加载到内存来,并且获得 一个hw_module_t接口来打开相应的设备. 函数h ...

  5. ETHNET DHCP的两种方式

    DHCP API: nx_dhcp_create nx_dhcp_start nx_dhcp_stop nx_dhcp_delete nx_ip_address_get //客户端IP获取 nx_dh ...

  6. winrm service

    今天看脚本忽然发现一个服务,叫winRM服务,这是个PowerShell的远程管理.开启它可以很大程度的方便用PowerShell操控! 下面是我找到的一些资料: 在Linux中,我们可以使用安全的S ...

  7. el表达式对js方法的传值

    我常用于在jsp页面遍历集合和分页中的页面跳转事件. jsp: <!-- 引入jstl --> <%@ taglib prefix="c" uri="h ...

  8. Codeforces Problem - 38E - Let's Go Rolling!(DP)

    E. Let's Go Rolling! time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. net.sf.json.JSONObject 和org.json.JSONObject

    参考 net.sf.json.JSONObject 和org.json.JSONObject 的差别

  10. 怎么设置myeclipse的格式化键格式每行的字符长度

    MyEclipse 格式化代码调整每行长度默认每行是80~对于咱初学者,非敏捷型编码者来说~有点短以至于Ctrl+Shift+F 格式化代码后总把一行拆成两行或者更多有点不适应 所以按个人爱好 更改他 ...