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 ...
随机推荐
- HDU2256&&HDU4565:给一个式子的求第n项的矩阵快速幂
HDU2256 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2256 题意:求(sqrt(2)+sqrt(3))^2n%1024是多少. 这个题算是h ...
- Linux 下的同步机制
2017-03-10 回想下最初的计算机设计,在单个CPU的情况下,同一时刻只能由一个线程(在LInux下为进程)占用CPU,且2.6之前的Linux内核并不支持内核抢占,当进程在系统地址运行时,能打 ...
- sql server数据库状态监控
sql server数据库监控 转自:https://www.cnblogs.com/seusoftware/category/500793.html 6. SQL Server数据库监控 - 如 ...
- 前端 javascript 数据类型 布尔类型
python 是大写 True javascript 是小写 true false 也是 布尔类型仅包含真假,与Python不同的是其首字母小写. == 比较值相等 != 不等于 ...
- 安装CentOS 7 文字版
下载镜像 http://mirrors.163.com/ CentOS 7.4 http://mirrors.163.com/centos/7.4.1708/isos/x86_64/ 选择 CentO ...
- java中使用MD5对密码进行加密
import org.springframework.security.authentication.encoding.MessageDigestPasswordEncoder; import org ...
- python16_day25【crm】
一.CRM模拟admin功能 1.过滤功能 2.显示数据分页 3.动态菜单 项目:https://github.com/willianflasky/growup/tree/master/s16/hom ...
- import MySQLdb 与 import mysql 有什么区别?
MySQLdb 只支持Python 2.* ,暂时还不支持3.* 可以用PyMySQL 代替,把__init__.py 中添加俩行: import pymysql pymysql.install_as ...
- Java 写文件实现换行
第一种: 写入的内容中利用\r\n进行换行 File file = new File("D:/text"); try { if(!file.exists()) file.creat ...
- XDU 1003 B进制加法(高精度)
#include<bits/stdc++.h> using namespace std; long long mpow(long long a,long long b) { ; ) ; w ...