LeetCode——Largest Rectangle in Histogram
Question
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 heights = [2,1,5,6,2,3],
return 10.
Solution 1
动态规划,但是会超时。
Code 1
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int num = heights.size();
if (num == 0)
return 0;
vector<vector<int>> min_heights(num, vector<int>(num, 0));
vector<vector<int>> max_areas(num, vector<int>(num, 0));
for (int i = 0; i < num; i++) {
min_heights[i][i] = heights[i];
max_areas[i][i] = heights[i] * 1;
}
for (int i = 2; i <= num; i++) {
for (int j = 0; j <= num - i; j++) {
min_heights[j][j + i - 1] = min(heights[j], min_heights[j + 1][j + i - 1]);
max_areas[j][j + i - 1] = max(min_heights[j][j + i - 1] * i, max_areas[j + 1][j + i - 1]);
max_areas[j][j + i - 1] = max(max_areas[j][j + i - 1], max_areas[j][j + i - 2]);
}
}
return max_areas[0][num - 1];
}
};
Solution 2
同样是动态规划,但是这次只需要记录之前的比自己低的坐标值。然后遍历之前比自己低的坐标值,每个低的都计算一次面积,具体看代码实现。
Code 2
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
// 记录比自己低的index
int res = 0;
height.push_back(0);
vector<int> index;
for (int i = 0; i < height.size(); i++) {
// 遍历所有比当前高度高的值,这也就是为什么会在height后面插入0的原因
while (index.size() > 0 && height[index.back()] >= height[i]) {
int h = height[index.back()];
index.pop_back();
// 这个是为了计算宽度值
int idx = index.size() > 0 ? index.back() : -1;
if (h * (i - idx - 1) > res)
res = h * (i - idx - 1);
}
index.push_back(i);
}
return res;
}
};
LeetCode——Largest Rectangle in Histogram的更多相关文章
- leetcode Largest Rectangle in Histogram 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- LeetCode: Largest Rectangle in Histogram 解题报告
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- LeetCode: Largest Rectangle in Histogram(直方图最大面积)
http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...
- [leetcode]Largest Rectangle in Histogram @ Python
原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...
- [LeetCode] Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- leetcode -- Largest Rectangle in Histogram TODO O(N)
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [LeetCode] Largest Rectangle in Histogram 解题思路
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
随机推荐
- _init_() got an unexpected keyword argument ‘shape’
按照<TensorFlow:实战Google深度学习框架>一书学习的tensorflow,书中使用的是0.9.0版本,而我安装的是1.11.0 如果按照书上的例子来,因为这本书使用tens ...
- xpath-grab english name
from scrapy.spider import Spider from scrapy.crawler import CrawlerProcess import pymysql conn = pym ...
- Spark 源码分析 -- BlockStore
BlockStore 抽象接口类, 关键get和put都有两个版本序列化, putBytes, getBytes非序列化, putValues, getValues 其中putValues的返回值为P ...
- POJ1470Closest Common Ancestors 最近公共祖先LCA 的 离线算法 Tarjan
该算法的详细解释请戳: http://www.cnblogs.com/Findxiaoxun/p/3428516.html #include<cstdio> #include<alg ...
- 调试maven源代码
下载源代码,导入idea 运行MavenCli ,设置vm参数 -Dclassworlds.conf=/Users/fsq/Downloads/apache-maven-3.6.2.0/bin/m2. ...
- dos常用命令【总结】
win7下有很多有用的dos命令,现在总结如下: 延伸:Linux常用命令[总结] 命令 作用 其他 ping 检查和另一台主机的连通性 ping 127.0.0.1 telnet 检查连通性 ...
- mysql 约束条件 auto_increment 自动增长 修改自增字段起始值
创建一张表 t20 mysql) ); Query OK, rows affected (0.01 sec) mysql> desc t20; +-------+----------+----- ...
- python web框架 MVC MTV
WEB框架 MVC Model View Controller 数据库 模板文件 业务处理 MTV Model Template View 数据库 模板文件 业务处理
- python学习笔记(二十四)继承与封装
继承(extends)就是把多个类中相同的成员给提取出来定义到一个独立的类中,然后让这多个类和该独立的类产生一个关系,这多个类就具备了这些类容,这个关系就叫做继承. 实现继承的类称为子类,也叫派生类, ...
- 《你的SSD可以用100年,你造吗?》总结
来自 http://www.ssdfans.com/?p=1778 上图是闪存的一个基本存储单元,由一种类NMOS的双层浮空栅 (Floating Gate) MOS管组成,用以存储一个bit(SLC ...