https://leetcode.com/problems/largest-rectangle-in-histogram/

https://leetcode.com/problems/maximal-rectangle/

 class Solution {
public:
int largestRectangleArea2(vector<int> height) {
if(height.size()==) return ; int ret = numeric_limits<int>::min();
for(int i=;i<height.size();++i) {
int area = height[i];
int left = i-, right = i+;
for(;left>= && height[i]<=height[left];--left) area += height[i];
for(;right<height.size() && height[i]<=height[right];++right) area += height[i];
ret = max(ret, area);
}
return ret;
}
int largestRectangleArea(vector<int>& height) {
//return largestRectangleArea2(height);
if(height.size()==) return ; height.push_back(-);
stack<int> st;
int ret = numeric_limits<int>::min(), hId, width, area, start;
for(int i=;i<height.size();++i) {
if(st.empty()) {
st.push(i);
start = st.top();
}
if(height[st.top()] < height[i]) st.push(i);
while(!st.empty() && height[st.top()] > height[i]) {
hId = st.top(); st.pop();
width = st.empty()? i-start: i-st.top()-;
ret = max(ret, width * height[hId]);
}
st.push(i);
}
return ret;
}
};
 class Solution {
public:
void clear(vector<int> &vec) {
for(int i=;i<vec.size();++i) vec[i] = ;
}
void clearStack(stack<int> &st) {
while(!st.empty()) st.pop();
}
int largestRectangleArea(vector<int>& height) {
if(height.size()==) return ; height.push_back(-);
stack<int> st;
int ret = numeric_limits<int>::min(), hId, width, area, start;
for(int i=;i<height.size();++i) {
if(st.empty()) {
st.push(i);
start = st.top();
}
if(height[st.top()] < height[i]) st.push(i);
while(!st.empty() && height[st.top()] > height[i]) {
hId = st.top(); st.pop();
width = st.empty()? i-start: i-st.top()-;
ret = max(ret, width * height[hId]);
}
st.push(i);
}
return ret;
}
int maximalRectangle(vector<vector<char>>& matrix) {
if(matrix.size() == ) return ; vector<int> height(matrix[].size()); int ret = numeric_limits<int>::min(), start;
for(int i=;i<matrix.size();++i) {
for(int j=;j<matrix[].size();++j) {
if(matrix[i][j]=='') height[j]++;
else height[j] = ;
} ret = max(ret, largestRectangleArea(height));
} return ret;
}
};

leetcode@ [84/85] Largest Rectangle in Histogram & Maximal Rectangle的更多相关文章

  1. LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle

    1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...

  2. 47. Largest Rectangle in Histogram && Maximal Rectangle

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

  3. 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形

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

  4. LeetCode (85): Maximal Rectangle [含84题分析]

    链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...

  5. LeetCode 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 85--最大矩形(Maximal Rectangle)

    84题和85五题 基本是一样的,先说84题 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 思路很简单,通过循环,分别判断第 i 个柱子能够延展的长度le ...

  6. LeetCode 84. Largest Rectangle in Histogram 单调栈应用

    LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...

  7. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

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

  8. [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形

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

  9. LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)

    题目描述 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的 ...

随机推荐

  1. js的原型链

    js中的原型链是一个很重要的概念,理解了原型链,对js程序的开发有很大的好处,废话不说,先上图: javascript是基于原型的语言,所以一个对象可以另一个对象继承.不过javascript实现的时 ...

  2. Java 另一道构造器与构造器重载的题目

    题目: 请写出以下程序的输出结果 public class ConstructorTest2 { public static void main(String[] args) { new B(&quo ...

  3. <context:component-scan>配置解析(转)

    在xml配置了这个标签后,spring可以自动去扫描base-pack下和其子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注 ...

  4. Eclipse不能自动编译 java文件的解决方案

    前段时间出现了eclipse 不自动编译java文件的问题,在网上找了好长时间,总算把问题解决了,现在把这个问题的解决方法总结一下. 1,看看project -- Build Automaticall ...

  5. codeforces #309 div1 C

    首先我们会发现所有的人构成了一个图 定义相爱为 在一个集合里 定义相恨为 不在一个集合里 很容易发现满足条件的图一定是一个二分图 那么分类讨论如下: 1.如果出现不合法 答案为0 2.如果不是一个二分 ...

  6. xcode 预编译头文件

    xcode 预编译头文件 cocos2d-prefix.pch  #import <Foundation/Foundation.h>

  7. HDU1171——Big Event in HDU(母函数)

    Big Event in HDU DescriptionNowadays, we all know that Computer College is the biggest department in ...

  8. python 中@property的使用

    从14年下半年开始接触到python,自学了一段时间,后又跟别人学习了下,把基础知识基本上学过了.忽然感觉python不可能这么简单吧,就这么点东西?后来看了下书,发现还有很多的高级部分.连续看了两天 ...

  9. git 日常使用

    git clone git checkout      git 删除 本地分支: git branch -d <本地分支名> git branch -D <本地分支名>(大写表 ...

  10. UVa 10214 (莫比乌斯反演 or 欧拉函数) Trees in a Wood.

    题意: 这道题和POJ 3090很相似,求|x|≤a,|y|≤b 中站在原点可见的整点的个数K,所有的整点个数为N(除去原点),求K/N 分析: 坐标轴上有四个可见的点,因为每个象限可见的点数都是一样 ...