Largest Rectangle in Histogram
来源:互联网 发布:零基础学qt4编程pdf 编辑:程序博客网 时间:2024/06/07 18:42
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.
For example,
Given heights = [2,1,5,6,2,3]
,
return 10
.
class Solution {public: int largestRectangleArea(vector<int>& heights);};int Solution::largestRectangleArea(vector<int>& heights){ int heightsSize = heights.size(); std::stack<int> s;int i = 0;int max = 0;while(i < heightsSize){if(s.empty() || heights[s.top()] <= heights[i]){s.push(i);i++;} else {int index = s.top();s.pop();int tmp_max = heights[index] *(s.empty() ? i : (i - s.top() - 1));if(max < tmp_max){max = tmp_max;}}}while(!s.empty()){int index = s.top();s.pop();int tmp_max = heights[index] *(s.empty() ? i : (i - s.top() - 1));if (max < tmp_max){max = tmp_max;}}return max;}
https://leetcode.com/problems/largest-rectangle-in-histogram/
http://www.geeksforgeeks.org/largest-rectangle-under-histogram/
0 0
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- Largest Rectangle in Histogram
- hdu_4417_Super Mario_TLE
- android shape的使用
- 51say weekend keyword scholl district house
- Mybaits学习过程中出现的错误
- leetcode 242. Valid Anagram
- Largest Rectangle in Histogram
- recycleview学习02
- 静态函数库和动态函数库的制作
- 进程的地址空间与函数调用过程
- 清除mysql表中数据
- hadoop伪分布式集群上传文件出现的位数问题
- 用javamail实现一个简单的邮件发送
- 《 Mining of Massive Datasets》学习笔记之 Finding Similar Items
- 107. Binary Tree Level Order Traversal II