LeetCode | Largest Rectangle in Histogram(直方图围城的最大矩形面积)
2014-08-12 20:05
441 查看
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 =
The largest rectangle is shown in the shaded area, which has area =
For example,
Given height =
return
题目解析:
方案一:
这道题目想到了动态规划,m[i,j] = min(
m[i,j-1]/(j-i) , height[j] ) * (j-i+1);设置动态设置二维数组,也是以len为主循环。最后遍历整个二维数组,找出最大值。方法正确,但提交的时候,内存超限。还是数组太大造成的。
方案二:
链接中用到栈的方法,各种各样,回头有时间了再深究……
http://www.cnblogs.com/remlostime/archive/2012/11/25/2787359.html
http://blog.sina.com.cn/s/blog_727d57100100o3e6.html
http://www.2cto.com/kf/201207/140484.html
/article/2749967.html
http://www.cnblogs.com/avril/archive/2013/08/24/3278873.html
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 =
10unit.
For example,
Given height =
[2,1,5,6,2,3],
return
10.
题目解析:
方案一:
这道题目想到了动态规划,m[i,j] = min(
m[i,j-1]/(j-i) , height[j] ) * (j-i+1);设置动态设置二维数组,也是以len为主循环。最后遍历整个二维数组,找出最大值。方法正确,但提交的时候,内存超限。还是数组太大造成的。
class Solution { public: int largestRectangleArea(vector<int> &height) { int n = height.size(); int **arr = new int* ; for(int i = 0;i < n;i++) arr[i] = new int ; for(int i = 0;i < n;i++) arr[i][i] = height[i]; for(int len = 2;len <= n;len++){ for(int i = 0;i < n-len+1;i++){ int j = i + len -1; int min = arr[i][j-1]/(j-i) > height[j] ? height[j] : arr[i][j-1]/(j-i); arr[i][j] = min * (j-i+1); } } int largest = 0; for(int i = 0;i < n;i++) for(int j = i;j < n;j++){ if(largest < arr[i][j]){ largest = arr[i][j]; } } for(int i = 0;i < n;i++) delete[] arr[i]; delete[] arr; return largest; } };
方案二:
链接中用到栈的方法,各种各样,回头有时间了再深究……
http://www.cnblogs.com/remlostime/archive/2012/11/25/2787359.html
http://blog.sina.com.cn/s/blog_727d57100100o3e6.html
http://www.2cto.com/kf/201207/140484.html
/article/2749967.html
http://www.cnblogs.com/avril/archive/2013/08/24/3278873.html
相关文章推荐
- [LeetCode]—Largest Rectangle in Histogram 求直方图最大填充矩形面积
- 解题报告:LeetCode Largest Rectangle in Histogram(计算最大矩形面积)
- Largest Rectangle in Histogram (直方图中最大面积) 【leetcode】
- [Leetcode] largest rectangle in histogram 直方图中最大的矩形
- LeetCode-Largest Rectangle in Histogram-直方图上的最大矩形-基于栈的滑动最值
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
- [C++]LeetCode: 133 Largest Rectangle in Histogram(最大矩形面积)
- LeetCode_直方图最大面积(Largest Rectangle in Histogram)
- LeetCode: Largest Rectangle in Histogram(直方图最大面积)
- [Leetcode] largest rectangle in histogram 直方图中最大的矩形
- LeetCode: Largest Rectangle in Histogram(直方图最大面积)
- 柱状图内最大的矩形面积 Largest Rectangle in Histogram @LeetCode
- 【直方图的最大面积】Largest Rectangle in Histogram
- Largest Rectangle in Histogram 一排矩形中的最大的面积
- HDU 1506 Largest Rectangle in a Histogram(最大矩形面积)
- 栈——largest-rectangle-in-histogram求柱形图中的最大矩形面积
- leetcode之Largest Rectangle in Histogram、Maximal Rectanglex;待字闺中之最大矩形分析
- LeetCode OJ 之 Largest Rectangle in Histogram (直方图中的最大矩形)
- 寻找直方图中的最大矩形 Largest Rectangle in Histogram
- HDU 1056 Largest Rectangle in a Histogram(dp)(求最大的矩形面积)