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. uva 567

    Floyd 算法   就输入麻烦点 #include <iostream> #include <cstring> #include <cstdlib> #inclu ...

  2. 服务器部署_linux下部署jprofiler简单备忘

    1.windows安装jprofiler 2.linux下安装jprofiler服务端,记好安装路径.假设是安装在/ex/bin/下 3. 配置tomcat的启动sh文件,在后面加入以下参数:  -a ...

  3. 图形学:图像围绕着某个点P(a,b)旋转------白话版

    前提:在研究图形时候,我们并没有规定图形的大小,所以任意图形多是支持的,这也另外说明了一点,图形转换和图形的大小没有关系. 如果图像围绕着某个点P(a,b)旋转,则先要将坐标系平移到该点,再进行旋转, ...

  4. ecos内核概览--bakayi译

    http://blog.csdn.net/wangzaiwei2006/article/details/6453423

  5. Android 遍历sdcard中指定文件夹下的图片(jpg,jpeg,png)

    File scanner5Directory = new File(Environment.getExternalStorageDirectory().getPath() + "/scann ...

  6. 2.1 linux中uboot移植

    -- --------------------------------------------------------------------------------------- (一)友善之臂介绍 ...

  7. HTML5学习(五)----SVG

    参考教程地址:http://www.w3school.com.cn/html5/html_5_svg.asp HTML5 支持内联 SVG. 什么是SVG? SVG 指可伸缩矢量图形 (Scalabl ...

  8. 生产环境上shell的解读

    一直以来对shell都不是很熟悉,只停留在基本的linux上操作上,这周因为定位问题接触到了生产环境上的脚本,因此作为引子学习一下.很多命令只是点到,等真正需要独立完成的时候再去学习. #!/bin/ ...

  9. SCOI2009windy数

    数位DP,还不怎么会…… 其中calc函数的计算分为三部分: 第一部分:统计最高位为0的情况,或者说不足最高位位数的数的个数 第二部分:统计最高位为1到a[len]-1的情况,直接调用数组即可 第三部 ...

  10. Excel 内容粘贴到DataGridView, DataGridView 粘贴到 Excel

    void dataGridView1_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Ke ...