又是一道构思巧妙的题,暴力求解复杂度太高,通过构造一个递增栈来解决:如果当前元素小于栈顶元素,则说明栈内已经构成一个

递增栈,则分别计算以每个元素为最低值的面积;反之,则入栈。

int largestRect(vector<int> &height)
{
stack<int> s;//定义一个单调递增栈
height.push_back();//定义单调递增栈的最后一个
int result = ;//记录当前最大的面积
for (int i = ; i < height.size();)//满足条件i才递增
{
if (s.empty() || height[i]>s.top())
{
s.push(i++);//只有当前元素大于栈顶元素是才入栈相应的序号,构造递增栈
}
else {
//当前元素小于栈顶元素时
int tmp = s.top();//保留栈顶元素
s.pop();//弹出直至当前元素大于栈顶元素,使栈仍然是递增的
//
result = max(result, height[tmp]*(s.empty() ? i : i - s.top() - ));
}
} return result;
}

注:上述代码有误,应该是height[s.top()]

Leetcode 之Largest Rectangle in Histogram(40)的更多相关文章

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

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

  2. leetcode之Largest Rectangle in Histogram

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

  3. Java for LeetCode 084 Largest Rectangle in Histogram【HARD】

    For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Hist ...

  4. 关于LeetCode的Largest Rectangle in Histogram的低级解法

    在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...

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

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

  6. LeetCode之Largest Rectangle in Histogram浅析

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

  7. [LeetCode OJ] Largest Rectangle in Histogram

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

  8. [LeetCode#84]Largest Rectangle in Histogram

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

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

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

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

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

随机推荐

  1. CF449C:Jzzhu and Apples——题解

    https://vjudge.net/problem/CodeForces-449C 题目大意:1-n编号的苹果两两一对,他们的最大公约数不为1,求这些对的最大匹配. ———————————————— ...

  2. 2067: [Poi2004]SZN——树上贪心+二分

    题目大意: 给一棵树.求用最少的链覆盖这棵树(链不能相交),在这个基础上求最长的链最短可以是多少. n<=10000 题解: 肯定先处理第一问: 答案:$\sum_(du[i]-1)/2+1$ ...

  3. CSS的历史与工作原理

    1. 浏览器的发展与CSS 网页浏览器主要通过HTTP协议连接网页服务器而取得网页,HTTP容许网页浏览器送交资料到网页服务器并且获取网页.目前最常用的 HTTP 是 HTTP/1.1,这个协议在RF ...

  4. snmp实用篇

    简单网络管理协议(SNMP)是 TCP/IP协议簇的一个应用层协议.在1988年被制定,并被Internet体系结构委员会(IAB)采纳作为一个短期的网络管理解决方案:由于 SNMP的简单性,在Int ...

  5. Codeforces Round #402 (Div. 2) A B C sort D二分 (水)

    A. Pupils Redistribution time limit per test 1 second memory limit per test 256 megabytes input stan ...

  6. Codeforces Round #341 (Div. 2)A

    A. Wet Shark and Odd and Even time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  7. HDU 3277 最大流+二分

    Marriage Match III Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  8. ObservableCollection 类

    假设您正在创建 Windows 窗体应用程序,并且已将 DataGridView 控件绑定到标准 List(Of Customer) 数据结构.您希望能够使网格中的项目与基础数据源中的值保持同步.也就 ...

  9. iframe的使用及操作

    一.iframe的使用方法: 在一个页面中加入iframe代码,例如: <div class="myiframe"> <iframe src="test ...

  10. JavaScript知识递归实现数组中指定后代元素的查找

    描述:要求将下面的数据类型中每个子孙后代id放入一个数组并返回 var arr = [ {"id":28,"text":"公司信息", &q ...