Largest Rectangle in Histogram
2015-07-30 22:56
337 查看
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
Solution:
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.
Solution:
class Solution { public: int largestRectangleArea(vector<int>& height) { int len = height.size(), maxarea = 0; vector<int> left(len); vector<int> right(len); for(int i = 0; i < len; ++i) { left[i] = i; while(left[i] > 0 && height[left[i]-1] >= height[i]) left[i] = left[left[i]-1]; } for(int i = len - 1; i >= 0; --i) { right[i] = i; while(right[i] < len - 1 && height[right[i]+1] >= height[i]) right[i] = right[right[i]+1]; } for(int i = 0; i < len; ++i) { maxarea = max(height[i] * (right[i] - left[i] + 1), maxarea); } return maxarea; } };
相关文章推荐
- 黑马程序员------面向对象
- springmvc中解决406问题
- 解决ubuntu中vi不能正常使用方向键与退格键的问题
- Spring_00600_IOC_annotation_Components
- 工作了大半年是否该继续程序员之路
- VLAN间通信之端到端模式
- VLAN间通信之端到端模式
- 函数的默认参数的大坑,参数的可变参数
- 各种情况的界面添加或弹出视图
- 从 NSURLConnection 到 NSURLSession
- Web 建站技术中,HTML、HTML5、XHTML、CSS、SQL、JavaScript、PHP、ASP.NET、Web Services 是什么?
- string的反转
- 我的人生荒废的那几年,其实就是从计划经济转变到市场经济的诸多不适应
- centos——安装jdk
- 随想:如果手机与手机之间具备了无线电测距测向功能
- Multi-University Training Contest 4 — 1002题
- 数学推导+高精度进制转换+解同余方程 POJ 3708
- 设计模式-简介
- Parse 和 Swift 搭建一个像 Instagram
- 素数回文