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 ...
随机推荐
- git 学习(3)文件删除恢复
git学习(3) 撤销编辑 如果我们在编辑版本a的时候,如果在没有add之前,发现需要重新编辑版本a怎么办呢,可以通过git reset --hard comm_id, commit_id是版本a的提 ...
- Fat URLs Client Identification
w在每个URL后面都附加一个用户特有的标识码. HTTP The Definitive Guide Some web sites keep track of user identity by gene ...
- Storm-源码分析-Topology Submit-Worker
1 mk-worker 和其他的daemon一样, 都是通过defserverfn macro来创建worker (defserverfn mk-worker [conf shared-mq-cont ...
- 剑指Offer——复杂链表的复制
题目描述: 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用, ...
- 神、上帝以及老天爷--hdu2048(错排,递推)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2048 1. N张字条的所有可能排列自然是N!(分母). 现在的问题就是求N张字条的错排数f(N)(分子 ...
- sVIrt概述
sVirt概述 前面已经对seLInux的基本原理做了分析,seLinux主要就是基于主体和客体的安全上下文,进行访问决策.那么安全上下文是不是又可以理解为一个标签呢? 基于以上seLInux的特性, ...
- 一次因为文件名开头包含空格而导致FTP文件一直无法下载的悲剧!
最近负责公司研究新的多渠道打包方案,之前的打包方案太慢了,因此采用了美团的Android Signature V2 Scheme签名下的新一代渠道包打包神器 方案进行了多渠道打包.但是由于马虎,在配置 ...
- Spark应用日志级别设置
一. 日志效率原因 开发时,控制台输出一大堆日志信息,严重影响查看日志效率. 从控制台输出日志我们可以看出,应用程序是默认加载Spark-core包下面的log4j-defaults.proper ...
- jQuery HTML操作学习笔记
学习资料 jQuery教程 获取 1.获取.设置元素的内容 1.1获取或设置目标元素的文本内容 语法 $(selector).text(); 获取元素文本内容 $(selector).text(con ...
- Maven打包部署
Maven打Jar包 问题一 Eclipse突然SB告诉我类不存在还让我导包进来O__O "…,但是我明明有这个类.这是不要慌,通过在网上搜索得知:由于eclipse的编译是基于时间戳的判断 ...