一、题目说明

题目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的更多相关文章

  1. 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...

  2. 84. Largest Rectangle in Histogram

    https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...

  3. LeetCode 84. Largest Rectangle in Histogram 单调栈应用

    LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...

  4. 【LeetCode】84. Largest Rectangle in Histogram

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  5. 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 ...

  6. 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 ...

  7. LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形

    原题 Given n non-negative integers representing the histogram's bar height where the width of each bar ...

  8. leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  9. 84. Largest Rectangle in Histogram *HARD* -- 求柱状图中的最大矩形面积

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

随机推荐

  1. copy and swap技巧与移动赋值操作符

    最近在实现一个Delegate类的时候碰到了一个问题,就是copy and swap技巧和移动赋值操作符有冲突. 比如有以下一个类: class Fun { public: Fun(const Fun ...

  2. CTRL-IKun团队选题报告

    1. 团队简介 1.1团队名称:CTRL-IKun 1.2队员学号列表 姓名 学号列表 廖志丹 201731032125 王川 201731021132 江天宇 201731024132 张微玖 20 ...

  3. linux--->ab测试工具使用

    ab测试工具使用 ab简介 是apache自带的压力测试工具.其原理是ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问.它的测试目标是基于URL的,因此,它既可以用来测试ap ...

  4. Linux 查看磁盘IO并找出占用IO读写很高的进程

    背景-线上告警 线上一台服务器告警,磁盘利用率 disk.util > 90,并持续告警. 登录该服务器后通过 iostat -x 1 10 查看了相关磁盘使用信息.相关截图如下: # 如果没有 ...

  5. Vue中的计算属性

    一.什么是计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护. 二.计算属性的用法 在一个计算属性里可以完成各种复杂的逻辑,包括运算.函 ...

  6. Marginalize

    在David M.Blei 的Distance Dependent Chinese Restaurant Processes 中提到:DDCRP 的一个重要性质,也是和dependent DP 的一个 ...

  7. Hello,world!一切的开始

    普及知识 当我们准备开发Java程序时,我们需要两样基础的工具--JDK与IDE.在这里需要解释一下什么是JDK还有IDE.JDK的全称是Java Development kit,即Java开发工具集 ...

  8. Java:多线程概述与创建方式

    目录 Java:多线程概述与创建方式 进程和线程 并发与并行 多线程的优势 线程的创建和启动 继承Thread类 start()和run() 实现Runnable接口 实现Callable接口 创建方 ...

  9. 《 Java 编程思想》CH05 初始化与清理

    < Java 编程思想>CH05 初始化与清理 用构造器确保初始化 在 Java 中,通过提供构造器,类的设计者可确保每个对象都会得到初始化.Java 会保证初始化的进行.构造器采用与类相 ...

  10. 视觉slam十四讲课后习题ch3-7

    题目回顾: 设有小萝卜一号和小萝卜二号位于世界坐标系中,小萝卜一号的位姿为:q1=[0.35,0.2,0.3,0.1],t2=[0.3,0.1,0.1]^T (q的第一项为实部.请你把q归一化后在进行 ...