刷题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中的super()函数详解
关于Python3中的super()函数 我们都知道,在Python3中子类在继承父类的时候,当子类中的方法与父类中的方法重名时,子类中的方法会覆盖父类中的方法, 那么,如果我们想实现同时调用父类和子 ...
- Basic Thought / Data Structure: 前缀和 Prefix Sum
Intro: 在OI中,前缀和是一种泛用性很高的数据结构,也是非常重要的优化思想 Function: 求静态区间和 模板题:输入序列\(a_{1..n}\),对于每一个输入的二元组\((l,r)\), ...
- Spring boot 学习中代码遇到的几个问题
1.报一大段红错 此时对象还是能创建成功的,解决方案参考链接https://blog.csdn.net/wanglin199709/article/details/99121487 2.无法创建对象 ...
- Centos7 下搭建STF平台
STF,全名Smartphone Test Farm---智能手机测试平台,可以提供远程真机调试的功能,目前仅支持Android设备. 环境准备 1.Node.js 8 安装Node.js $ cur ...
- kubernetes安装-kubeadm
系统信息 角色 系统 CPU Core memory master 18.04.1-Ubuntu 4 8G slave 18.04.1-Ubuntu 4 4G 安装前准备(主节点和从节点都需要执行) ...
- C++类中拷贝构造函数详解
a. C++标准中提到"The default constructor, copy constructor and copy assignment operator, and destruc ...
- data structure test
1.设计算法,对带头结点的单链表实现就地逆置.并给出单链表的存储结构(数据类型)的定义. #include <iostream> #include <cstdlib> #inc ...
- usaco1.1
Your Ride Is Here #include <iostream> #include <string> #include <vector> using na ...
- JavaScript 与 Java 有什么不同?
JavaScript 编程语言是由 Netscape,Inc. 开发的,它并不是 Java 平台的一部分. JavaScript 不会创建小应用程序或独立应用程序.在最常见的形式中,JavaScrip ...
- P4174 [NOI2006]最大获利 (最大权闭合子图)
P4174 [NOI2006]最大获利 (最大权闭合子图) 题目链接 题意 建\(i\)站台需要\(p_i\)的花费,当\(A_i,B_i\)都建立时获得\(C_i\)的利润,求最大的利润 思路 最大 ...