【Lintcode】122.Largest Rectangle in Histogram
题目:
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].

The largest rectangle is shown in the shaded area, which has area = 10 unit.
题解:
做编程题一定要自己AC了再去看discussion,之前一直草草的刷题,感觉忘得好快,尤其是边界条件的处理上一点长进也没有,这次hard题做了半个多小时,从开始看题到提交(提交了3次,都是小错误。。。太不细心了,这更加说明了白板编程的重要性,不要用IDE),完全自己码出来,以后会坚持这样做,否则去面试什么的肯定跪,也不利于思维逻辑能力的提高。
Solution 1 ()
class Solution {
public:
int largestRectangleArea(vector<int> height) {
if (height.empty()) return ;
stack<int> high;
int maxArea = ;
high.push();
height.push_back();
for (int i = ; i < height.size(); ++i) {
while (!high.empty() && height[i] < height[high.top()]) {
int index = high.top();
high.pop();
if (high.empty()) {
maxArea = max((i - ) * height[index], maxArea);
} else {
maxArea = max((i - high.top() - ) * height[index], maxArea);
}
}
high.push(i);
}
return maxArea;
}
};
Solution 2 ()
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
int ret = ;
height.push_back();
vector<int> index;
for(int i = ; i < height.size(); i++) {
while(index.size() > && height[index.back()] >= height[i]) {
int h = height[index.back()];
index.pop_back();
int sidx = index.size() > ? index.back() : -;
if(h * (i-sidx-) > ret)
ret = h * (i-sidx-);
}
index.push_back(i);
}
return ret;
}
};
Diveide and Conquer 思想比较容易懂, 就是写起来的时候边界条件有点麻烦。
Solution 3 ()
class Solution {
int maxCombineArea(const vector<int> &height, int s, int m, int e) {
// Expand from the middle to find the max area containing height[m] and height[m+1]
int i = m, j = m+;
int area = , h = min(height[i], height[j]);
while(i >= s && j <= e) {
h = min(h, min(height[i], height[j]));
area = max(area, (j-i+) * h);
if (i == s) {
++j;
}
else if (j == e) {
--i;
}
else {
// if both sides have not reached the boundary,
// compare the outer bars and expand towards the bigger side
if (height[i-] > height[j+]) {
--i;
}
else {
++j;
}
}
}
return area;
}
int maxArea(const vector<int> &height, int s, int e) {
// if the range only contains one bar, return its height as area
if (s == e) {
return height[s];
}
// otherwise, divide & conquer, the max area must be among the following 3 values
int m = s + (e-s)/;
// 1 - max area from left half
int area = maxArea(height, s, m);
// 2 - max area from right half
area = max(area, maxArea(height, m+, e));
// 3 - max area across the middle
area = max(area, maxCombineArea(height, s, m, e));
return area;
}
public:
int largestRectangleArea(vector<int> &height) {
if (height.empty()) {
return ;
}
return maxArea(height, , height.size()-);
}
};
为什么下面的代码过不了???
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
if (height.empty()) return ;
return maxArea(height, , height.size() - );
}
int maxArea(vector<int> height, int begin, int end) {
if (begin == end) {
return height[begin];
}
int mid = begin + (end - begin) / ;
int mArea = maxArea(height, begin, mid);
mArea = max(mArea, maxArea(height, mid + , end));
mArea = max(mArea, maxCombineArea(height, begin, mid, end));
return mArea;
}
int maxCombineArea(vector<int> height, int begin, int mid, int end) {
int maxArea = ;
int left = mid, right = mid + ;
int high = min(height[left], height[right]);
while (left >= begin && right <= end) {
high = min(high, min(height[left], height[right]));
maxArea = max(maxArea, (right - left + ) * high);
if (left == begin) {
++right;
} else if (right == end) {
--left;
} else {
if (height[left - ] > height[right + ]) {
--left;
} else {
++right;
}
}
}
return maxArea;
}
};
【Lintcode】122.Largest Rectangle in Histogram的更多相关文章
- 【LeetCode】84. Largest Rectangle in Histogram
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
- 【LeetCode】084. Largest Rectangle in Histogram
题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...
- 【LeetCode】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 85. Maximal Rectangle
问题描述: 84:直方图最大面积. 85:0,1矩阵最大全1子矩阵面积. 问题分析: 对于84,如果高度递增的话,那么OK没有问题,不断添加到栈里,最后一起算面积(当然,面积等于高度h * disPo ...
- 【一天一道LeetCode】#84. Largest Rectangle in Histogram
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...
- 【题解】hdu1506 Largest Rectangle in a Histogram
目录 题目 思路 \(Code\) 题目 Largest Rectangle in a Histogram 思路 单调栈. 不知道怎么描述所以用样例讲一下. 7 2 1 4 5 1 3 3 最大矩形的 ...
- 【HDOJ】1506 Largest Rectangle in a Histogram
Twitter还是Amazon拿这个题目当过面试题.DP区间求面积. /* 1506 */ #include <cstdio> #include <cstring> #incl ...
- Java for LeetCode 084 Largest Rectangle in Histogram【HARD】
For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Hist ...
随机推荐
- 设置mysql隔离级别
1.查看当前会话隔离级别 select @@tx_isolation; 2.查看系统当前隔离级别 select @@global.tx_isolation; 3.设置当前会话隔离级别 set sess ...
- 写一段代码,判断一个包括'{','[','(',')',']','}'的表达式是否合法(注意看样例的合法规则。) 给定一个表达式A,请返回一个bool值,代表它是否合法。
这道题比较奇怪,它的匹配规则并不是我们平时想想的那种匹配规则,例如:平时的匹配规则是().{}.[]才能匹配,本题中(和} .].)都能匹配.所以做题时要好好审题.另外,本题中给的测试用例是错误的. ...
- KVM+VNC 虚拟机远程管理
1.安装kvm grep -E -o 'vmx|svm' /proc/cpuinfo #检查服务器是否支持虚拟化(vmx为interl平台.svm是AMD平台) #安装KVM所需软件包: yum gr ...
- JQuery利用选择器定位动态id?
假如我们需要去定位一个动态生成的div,我们需要为它指定一个动态的id 例如: 前台使用EL进行迭代LIST生成div,为其添加动态的id,生成之后变成下面样式 <div id="tz ...
- mongo的时间类型,erlang中对其的处理
需求:要想在一个调度中,从mongo中查出大于一个时间戳的所有的数据总和. 这个需求很简单,一个是scheduler,还有另一个就是查出来大于某个时间戳的总和,比如大于每天0点时间点的和. 需要注意的 ...
- Angualr 实现复选框全选功能
html <html lang="en"> <head> <meta charset="UTF-8"> <title& ...
- Maven中央仓库地址(实用版)
最近做项目的时候,一直发现常用的oschina maven源一直都没有反应,后面发现原来oschina竟然关闭了maven源服务,后面经同事推荐了阿里云的maven源,这速度杠杠的 Maven 中央仓 ...
- 【NOI2015】【程序自己主动分析】【并查集+离散化】
Description 在实现程序自己主动分析的过程中,经常须要判定一些约束条件能否被同一时候满足. 考虑一个约束满足问题的简化版本号:如果x1,x2,x3,-代表程序中出现的变量.给定n个形如xi= ...
- 输出 pdf
jar 包 :core-renderer.jar iText-2.0.8.jar iTextAsian.jar 方式1: import java.io.FileNotFoundException ...
- CentOS 7 安装、配置、使用 PostgreSQL 9.5(一)安装及基础配置
一直不知道怎么读这个数据库的名字,在官网上找到了文档.PostgreSQL is pronounced Post-Gres-Q-L. 读音 What is PostgreSQL? How is it ...