【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 ...
随机推荐
- android开发系列之使用xml自定义控件
在android开发的过程中,有的时候面对多个Activity里面一些相同的布局,我们需要写多次相同的代码,同时这种方法给我们的项目维护也带来了很大不便.那么有没有一种可行的办法能够将Activity ...
- Cocos2d-x粒子系统
CCparticleSystem类封装实现对粒子的控制与调度,当中操作包含有: 1.产生粒子 2.更新粒子状态 3.回收无效的粒子 CCparticleSystem派生出CCParticleSyste ...
- Spark源码分析之四:Stage提交
各位看官,上一篇<Spark源码分析之Stage划分>详细讲述了Spark中Stage的划分,下面,我们进入第三个阶段--Stage提交. Stage提交阶段的主要目的就一个,就是将每个S ...
- _DataStructure_C_Impl:Floyd算法求有向网N的各顶点v和w之间的最短路径
#include<stdio.h> #include<stdlib.h> #include<string.h> typedef char VertexType[4] ...
- ejabberd日志分析客户端登录流程
通过ejabberd的日志,整理了下客户端登录流程. 1. 通过TCP连接5222端口的流程: (1) 客户端向服务器发送stream流 <stream:stream to="nba. ...
- mnesia的脏写和事物写的测试
在之前的文章中,测试了脏读和事物读之间性能差别,下面测试下脏写和事物写之间的性能差别: 代码如下: -module(mnesia_text). -compile(export_all). -recor ...
- JAVA程序开发按位运算的记录
忘记在哪里看到一个面试题:把int a,b的值互换,不能使用临时变量.刚开始完全懵逼,脑子里面全是浆糊,不知道如何下手.查看答案后猛地一惊,心想居然还有这种操作,真是叹为观止,真的感觉自己的基础是如此 ...
- PowerBuilder -- 指定重复的列不显示
- ADO.NET Data Service
关于ADO.NET Entity Framework部分的内容见ADO.NET Entity Framework(1-4) http://www.cnblogs.com/foundation/arch ...
- C#高级编程 第十五章 反射
(二)自定义特性 使自定义特性非常强大的因素时使用反射,代码可以读取这些元数据,使用它们在运行期间作出决策. 1.编写自定义特性 定义一个FieldName特性: [AttributeUsage(At ...