【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 ...
随机推荐
- 牛牛有一个鱼缸。鱼缸里面已经有n条鱼,每条鱼的大小为fishSize[i] (1 ≤ i ≤ n,均为正整数),牛牛现在想把新捕捉的鱼放入鱼缸。鱼缸内存在着大鱼吃小鱼的定律。经过观察,牛牛发现一条鱼A的大小为另外一条鱼B大小的2倍到10倍(包括2倍大小和10倍大小),鱼A会吃掉鱼B。考虑到这个,牛牛要放入的鱼就需要保证:1、放进去的鱼是安全的,不会被其他鱼吃掉 2、这条鱼放进去也不能吃掉其他鱼
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点. // #include<vector> #include<algorithm> #inc ...
- 09-redis事务及锁应用
Redis 中的事务 Redis支持简单的事务 Redis与 mysql事务的对比 ------------------------------------------------------- My ...
- vs中使用M_PI的问题及解决 角度转弧度&根据弧度计算圆周上点的坐标的方法
M_PI 是一个宏定义,圆周率的定义 C/C++ code #define M_PI 3.14159265358979323846 此宏定义和编译器有关,TC中M_PI宏就定义在& ...
- 开源项目Universal Image Loader for Android 说明文档 (1) 简单介绍
When developing applications for Android, one often facesthe problem of displaying some graphical ...
- python的安装及matplotlib安装
本文通过实践,自行安装了一遍python及matplotlib. 1.用python2.7的最新版本(写本文时,用的2.7.13).因为默认有安装pip,记得安装时选择最后一个添加环境变量,不然还要手 ...
- 【BZOJ3544】[ONTAK2010]Creative Accounting 前缀和+set
[BZOJ3544][ONTAK2010]Creative Accounting Description 给定一个长度为N的数组a和M,求一个区间[l,r],使得(\sum_{i=l}^{r}{a_i ...
- WCF基础之设计和实现服务协定
本来前面还有一个章节“WCF概述”,这章都是些文字概述,就不“复制”了,直接从第二章开始. 当然学习WCF还是要些基础的.https://msdn.microsoft.com/zh-cn/hh1482 ...
- 扩容数据盘_Linux
扩容数据盘_Linux_扩容云盘_云盘_用户指南_云服务器 ECS-阿里云 https://help.aliyun.com/document_detail/25452.html 磁盘扩容付费后: 在控 ...
- 给this添加属性
const f =()=>{ console.log(this) let a=5 console.log(this) console.log(this) this.ak =3} f() let ...
- multiple-value uuid.NewV4() in single-value context
[root@t ~]# go get github.com/aliyun/aliyun-sts-go-sdk/sts# github.com/aliyun/aliyun-sts-go-sdk/sts/ ...