刷题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 ...
随机推荐
- python3 密码字典匹配wifi密码,有界面
界面: 需要先安装一下pywifi模块 代码: # coding:utf-8 from tkinter import * from tkinter import ttk import pywifi f ...
- 19_07_07校内训练[xor]
题意 长度为n的数组,上面有k个位置是1,现在有l种长度的连续全1串,要求用最少的次数将这个数组异或成全0的数组.n<=1E5,k<=10,l<=100. 思考 先将数组进行异或的差 ...
- linux下误清用户/home下的文件怎么办?
2016-08-19 10:38:10 有时候我们不小心把home目录下的用户目录删除了,出现上图情况,每行开头直接变成-bash-3.2$这种形式而不是[lyp@centos7 ~]$这种,这时 ...
- 工具之sort
转自:http://www.cnblogs.com/dong008259/archive/2011/12/08/2281214.html sort命令是帮我们依据不同的数据类型进行排序,其语法及常用参 ...
- mac电脑下使用fcrackzip破解zip压缩文件密码
fcrackzip简介 fcrackzip是一款专门破解zip类型压缩文件密码的工具,工具小巧方便.破解速度快,能使用字典和指定字符集破解,适用于linux.mac osx 系统 fcrackzip安 ...
- geojson转esriJson
因为一些特殊需求,需要将geojson转为shp数据,网上有一些转换网站,但是存在一些问题,例如中文乱码.文件大小限制等等,折腾了一下,还是觉得用arcgis转比较好,因此先将geojson转为esr ...
- Word文档分节设置页码
在一篇论文中需要将摘要和目录作为一部分设置罗马数字页码,正文部分设置阿拉伯数字页码. 大致效果如下图所示: 这里面用到了分节符,步骤如下: 1 :点击开始菜单栏下 显示/隐藏编辑标记 2:点击插入菜单 ...
- 手把手实操教程!使用k3s运行轻量级VM
前 言 k3s作为轻量级的Kubernetes发行版,运行容器是基本功能.VM的管理原本是IaaS平台的基本能力,随着Kubernetes的不断发展,VM也可以纳入其管理体系.结合Container和 ...
- CCF_ 201312-3_最大的矩形
遍历数组中每一元素,左右延伸得出宽度. #include<iostream> #include<cstdio> using namespace std; int main() ...
- 《剑指Offer》第二章(一)题 9 -12
第二章 面试题9:用两个栈实现队列 题目:如面试题,给你两个栈, 实现队列的先进先出,即在队列头删除一个元素以及在队列的尾部添加一个元素 思路:这个题的分析感觉很巧妙,从一个具体的例子入手,找出其中的 ...