刷题84. Largest Rectangle in Histogram
一、题目说明
题目84. Largest Rectangle in Histogram,给定n个非负整数(每个柱子宽度为1)形成柱状图,求该图的最大面积。题目难度是Hard!
二、我的解答
这是一个 看起来容易,做起来很容易错的题目。我开始用的是“挖坑法”,遗憾的是总是Time Limit Exceeded。经过10次优化,还是很难看。
class Solution{
public:
int largestRectangleArea(vector<int>& heights){
int len = heights.size();
if(len<1) return 0;
if(len==1) return heights[0];
int result = 0;
int start=len-1,end=0;
int cnt = 0,sum=0;
int min;
while(1){
min = INT_MAX;
for(int i=0;i<len;i++){
if(heights[i]>0 && heights[i]<min){
min = heights[i];
}
}
//找到第1个正的
while(end<len && heights[end]<=0){
end++;
}
while(start>0 && heights[start]<=0){
start--;
}
if(start==end){
sum = heights[start];
if(sum>result) result = sum;
break;
}else if(end>start) {
break;
}
sum = 0;
cnt = 0;
//一次遍历,求大于0的连续长度 最大值
while(end<len){
cnt = 0;
sum = 0;
while(end<len && heights[end]>=min){
if(heights[end]==min) heights[end] = 0;
cnt++;
end++;
}
sum = cnt * min;
if(sum>result) result = sum;
if(end<len) end++;
}
end = 0;
start = len-1;
}
return result;
}
};
性能:
Runtime: 1536 ms, faster than 4.53% of C++ online submissions for Largest Rectangle in Histogram.
Memory Usage: 9.9 MB, less than 94.29% of C++ online submissions for Largest Rectangle in Histogram.
还能想到的方法就是暴力计算法,性能也差不多:
class Solution{
public:
//brute force
int largestRectangleArea(vector<int>& heights){
int len = heights.size();
if(len<1) return 0;
if(len==1) return heights[0];
int result = 0;
bool allthesame = true;
for(int i=0;i<len-1;i++){
if(heights[i]!=heights[i+1]){
allthesame = false;
}
}
if(allthesame){
return heights[0]*len;
}
for(int i=0;i<len;i++){
int minHeight = INT_MAX;
for(int j=i;j<len;j++){
minHeight = min(minHeight,heights[j]);
result = max(result,minHeight * (j-i+1));
}
}
return result;
}
};
Runtime: 1040 ms, faster than 5.03% of C++ online submissions for Largest Rectangle in Histogram.
Memory Usage: 10 MB, less than 94.29% of C++ online submissions for Largest Rectangle in Histogram.
三、优化措施
用单调递增stack法,代码如下:
class Solution{
public:
//单调递增栈
int largestRectangleArea(vector<int>& heights){
int result = 0;
stack<int> st;
st.push(-1);//-1 放进栈的顶部来表示开始
//按照从左到右的顺序,我们不断将柱子的序号放进栈中,直到 heights[i]<heights[st.top]
//将栈中的序号弹出,直到heights[stack[j]]≤heights[i]
for(int i=0;i<heights.size();i++){
while(st.top()!=-1 && heights[i]<heights[st.top()]){
int h = st.top();
st.pop();
result = max(result,heights[h]*(i - st.top() -1));
}
st.push(i);
}
// 遍历完了,但是没计算完
while(st.top() != -1){
int h = st.top();
st.pop();
int len = heights.size() - st.top() -1;
result = max(result,heights[h]*len);
}
return result;
}
};
Runtime: 16 ms, faster than 53.51% of C++ online submissions for Largest Rectangle in Histogram.
Memory Usage: 10.4 MB, less than 91.43% of C++ online submissions for Largest Rectangle in Histogram.
继续优化:
class Solution{
public:
//单调递增栈 ,借用i当栈
int largestRectangleArea(vector<int>& heights){
int result = 0;
int len, wid;
for (int i = 0; i < heights.size(); i++) {
if(i != heights.size() - 1 && heights[i] <= heights[i + 1]) continue; //这一步的判断很玄妙
wid = heights[i];
for (int j = i; j >= 0; j--) {
len = i - j + 1;
wid = min(wid, heights[j]);
result = max(result, len * wid);
}
}
return result;
}
};
Runtime: 12 ms, faster than 89.13% of C++ online submissions for Largest Rectangle in Histogram.
Memory Usage: 10 MB, less than 94.29% of C++ online submissions for Largest Rectangle in Histogram.
Next challenges:
刷题84. Largest Rectangle in Histogram的更多相关文章
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
- 84. Largest Rectangle in Histogram
https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- 【LeetCode】84. Largest Rectangle in Histogram
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形
1. Given n non-negative integers representing the histogram's bar height where the width of each bar ...
- LeetCode OJ 84. Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形
原题 Given n non-negative integers representing the histogram's bar height where the width of each bar ...
- leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- 84. Largest Rectangle in Histogram *HARD* -- 求柱状图中的最大矩形面积
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
随机推荐
- Ogre3d 1.7.x 的 RTShaderSystem的一个BUG
来源:http://www.ogre3d.org/forums/viewtopic.php?f=2&t=63644 表现:使用dx的shader可能会造成程序崩溃. 在文件OgreShader ...
- NetBeans8.2 修改代码补全延迟时间
NetBeans早期版本提供修改提示速度的选项,大概是6.5版本之后就没这个修改功能了,但是仍可以配置: Windows配置如下: 1.C:\Users\${用户名}\AppData\Roaming\ ...
- 使用AOP和Semaphore对项目中具体的某一个接口进行限流
整体思路: 一 具体接口,可以自定义一个注解,配置限流量,然后对需要限流的方法加上注解即可! 二 容器初始化的时候扫描所有所有controller,并找出需要限流的接口方法,获取对应的限流量 三 使用 ...
- Java使用自定义类加载器实现热部署
热部署: 热部署就是在不重启应用的情况下,当类的定义即字节码文件修改后,能够替换该Class创建的对象.一般情况下,类的加载都是由系统自带的类加载器完成,且对于同一个全限定名的java类,只能被加载一 ...
- python书籍推荐以及杂七杂八
黄哥推荐学习Python 10本好书 Python3网络爬虫开发实战 github daily-reading 想对大家说的话 POJ 算法练习 http://openjudge.cn/ http:/ ...
- Python 代码风格规范(Google)
Python风格规范 分号 tip 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 tip 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. Py ...
- 2018icpc南京现场赛-I Magic Potion(最大流)
题意: n个英雄,m个怪兽,第i个英雄可以打第i个集合里的怪兽,一个怪兽可以在多个集合里 有k瓶药水,每个英雄最多喝一次,可以多打一只怪兽,求最多打多少只 n,m,k<=500 思路: 最大流, ...
- Codeforces 922 C - Robot Vacuum Cleaner (贪心、数据结构、sort中的cmp)
题目链接:点击打开链接 Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that ...
- 《Python学习手册 第五版》 -第1章 问答环节
第一章的主要内容是解疑答惑的,这个部分也是很适合初学者的,回答了大部分初学者所关注的问题 1.为什么使用Python,或者说Python的优点, 作者是分为两个部分来谈的,人们使用中的经验总结以及Py ...
- java3选择结构一
1 public class jh_01_为什么需要if选择结构 { 2 /* 3 * 让它有条件性的去执行某些内容. 4 * System.out.println(2); 5 * 把你要控制的内容放 ...