题目:

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

  1. 【LeetCode】84. Largest Rectangle in Histogram

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

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

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

  3. 【LeetCode】084. Largest Rectangle in Histogram

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

  4. 【LeetCode】84. Largest Rectangle in Histogram——直方图最大面积

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

  5. 【Leetcode】84. Largest Rectangle in Histogram 85. Maximal Rectangle

    问题描述: 84:直方图最大面积. 85:0,1矩阵最大全1子矩阵面积. 问题分析: 对于84,如果高度递增的话,那么OK没有问题,不断添加到栈里,最后一起算面积(当然,面积等于高度h * disPo ...

  6. 【一天一道LeetCode】#84. Largest Rectangle in Histogram

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...

  7. 【题解】hdu1506 Largest Rectangle in a Histogram

    目录 题目 思路 \(Code\) 题目 Largest Rectangle in a Histogram 思路 单调栈. 不知道怎么描述所以用样例讲一下. 7 2 1 4 5 1 3 3 最大矩形的 ...

  8. 【HDOJ】1506 Largest Rectangle in a Histogram

    Twitter还是Amazon拿这个题目当过面试题.DP区间求面积. /* 1506 */ #include <cstdio> #include <cstring> #incl ...

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

随机推荐

  1. SOCKIT 在make时出现(target pattern contains no % stop)???

    Make错误(***target pattern contains no % stop) 1.   问题描述 在按照SoC_SW_Lab_13.0.pdf操作时候出现了下列图片的错误 2.   Bsp ...

  2. Xenomai 3 migration

    Xenomai 3 的rtdm驱动更像一般的Linux驱动,named device会在/dev/rtdm/xxx创建一个设备文件.而用户空间使用时,写得来也和Linux的一般char设备相似,ope ...

  3. Spark Streaming和Kafka整合开发指南(二)

    在本博客的<Spark Streaming和Kafka整合开发指南(一)>文章中介绍了如何使用基于Receiver的方法使用Spark Streaming从Kafka中接收数据.本文将介绍 ...

  4. 扒一扒P2P风控的底牌(转)

    互联网金融,这里面水就太深了,能当理财买的一般有两类,一个是货币基金,比如余额宝,这个大家已经十分清楚了,没什么风险, 但问题就是收益越来越低.实在是不过瘾了.而另外一种就是P2P理财了,收益很高,也 ...

  5. redis客户端连接,最大连接数查询与设置

    ##redis客户端连接数 redis通过监听一个TCP端口或socket的方式接收来自客户端的连接, 当与客户端建立连接后,redis内部会进行如下操作:()客户端socket会被设置为非阻塞模式, ...

  6. Python学习总结之五 -- 入门函数式编程

    函数式编程 最近对Python的学习有些怠慢,最近的学习态度和学习效率确实很不好,目前这种病况正在好转. 今天,我把之前学过的Python中函数式编程简单总结一下,分享给大家,也欢迎并感谢大家提出意见 ...

  7. ubuntu 中文显示乱码问题 (转)

    添加中文字符编码: $sudo vim /var/lib/locales/supported.d/local #添加下面的中文字符集 zh_CN.GBK GBK zh_CN.GB2312 GB2312 ...

  8. 转载 ----MAC 上搭建lua

    MAC 上搭建lua   其实mac上搭建lua环境,google上大把资料,我只是整合一下,因为小弟搭建的时候确实碰到一些问题. 下载和安装lua:(转自这里) 1.  下载最新版的lua-5.2. ...

  9. 【SSH进阶之路】Hibernate映射——一对一单向关联映射(五)

    [SSH进阶之路]Hibernate基本原理(一) ,小编介绍了Hibernate的基本原理以及它的核心,採用对象化的思维操作关系型数据库. [SSH进阶之路]Hibernate搭建开发环境+简单实例 ...

  10. sql查询的日期判断问题

    在SQLSERVE中,如果某个数据表的类型被定义成datetime类型,那么数据是包含秒的.这时候如何查询某天的数据呢?新手们可能想:最直接的做法是把时间部分去掉,只取日期部分.于是日期的函数就用上了 ...