刷题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 ...
随机推荐
- python数据类型之字典操作
Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型.一.创建字典字典由key和value成对组成.基本语法如下: infos = {"name&q ...
- golang中基本类型存储大小和转换
Go语言的基本类型有: bool string int.int8.int16.int32.int64 uint.uint8.uint16.uint32.uint64.uintptr byte // u ...
- ArrayList.subList方法使用总结
ArrayList.subList方法使用总结 示例 List<String> list=new ArrayList<>(); list.add("d"); ...
- 解决keil5不支持Samsung 2440A等较旧型号Soc的方法
手上有一块mini2440,最近想拿来跑一下程序.于是去下了最新版的Keil5,结果发现创建工程时没有Samsung一系列型号的CPU选项. 没有办法,去网上找了很久,才发现原因是新版的Keil5 ...
- 如何把您现有的流程系统数据迁徙到驰骋BPM上?
关键词:流程系统版本切换\ 驰骋BPM. 如何把您现有的流程系统数据迁徙到驰骋BPM上?这是一个大问题.在处理改问题之前,首先要了解驰骋bpm的数据库结构,驰骋bpm的特性在做计划. 驰骋BPM的流程 ...
- linux系统下gdb的简单调试
当我们写完程序后,我们会运行程序,在这个过程中,可能程序会出现错误. 我们可以利用gdb调试去看我们运行的程序,并且我们新手通过gdb调试能更好地去读懂 别人的程序.让我们更好的学习. 我们看下面这条 ...
- linux下的 $ ./configure $ sudo make && sudo make install
$ ./configure $ sudo make && sudo make install 1)./configure命令就是执行当前目录的名为configure的脚本,主要的作用是 ...
- python 中的反斜杠匹配的问题
关于反斜杠的匹配问题可以参考: https://www.cnblogs.com/mzc1997/p/7689235.html 文章中提出了两个概念: 字符串转义和正则转义,我觉得是理解反斜杠的关键所在 ...
- ELK:收集Docker容器日志
简介 之前写过一篇博客 ELK:日志收集分析平台,介绍了在Centos7系统上部署配置使用ELK的方法,随着容器化时代的到来,容器化部署成为一种很方便的部署方式,收集容器日志也成为刚需.本篇文档从 容 ...
- bat常用符合和for语句等
一.开头 @echo off(默认是echo on)@echo off执行以后,后面所有的命令均不显示,包括本条命令 二.特殊符号 1. | 命令管道符,echo Y|rd /s c:\abc,通过管 ...