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.

Example:

Input: [2,1,5,6,2,3]
Output: 10

84. Largest Rectangle in Histogram的升级版: 按行按列分别做动态规划

class Solution {
public int maximalRectangle(char[][] matrix) {
if(matrix.length == 0 ) return 0; int width = matrix[0].length;
int height = matrix.length;
int maxArea = 0;
int area; //按列动态规划,求出到此位置最多连续为1的次数
int[][] dp = new int[height][width];
for(int i = 0; i < width; i++){
for(int j = 0; j < height; j++){
dp[j][i] = matrix[j][i] - '0';
if(matrix[j][i] == '1' && j > 0){
dp[j][i] += dp[j-1][i];
}
}
} //按行动态规划,求出最大面积
for(int i = 0; i < height; i++){
area = largestRectangleArea(dp[i]);
if(area > maxArea) maxArea = area;
}
return maxArea;
} public int largestRectangleArea(int[] heights) {
Stack<Integer> st = new Stack<Integer>();
if(heights.length == 0) return 0; int leftIndex;
int topIndex;
int area;
int maxArea = 0;
int i = 0;
while(i < heights.length){
if(st.empty() || heights[i] >= heights[st.peek()]){
st.push(i);
i++;
}
else{
topIndex = st.peek();
st.pop();
leftIndex = st.empty()?0:st.peek()+1; //如果是空,说明从头开始的所有高度都比heights[topIndex]高
area = ((i-1)-leftIndex + 1) * heights[topIndex];
if(area > maxArea) maxArea = area;
}
}
while(!st.empty()){ //没有比栈顶元素小的元素让它出栈
topIndex = st.peek();
st.pop();
leftIndex = st.empty()?0:st.peek()+1;
area = ((i-1)-leftIndex + 1) * heights[topIndex];
if(area > maxArea) maxArea = area;
}
return maxArea;
}
}

85. Maximal Rectangle (JAVA)的更多相关文章

  1. 85. Maximal Rectangle

    85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...

  2. 刷题85. Maximal Rectangle

    一.题目说明 题目,85. Maximal Rectangle,计算只包含1的最大矩阵的面积.难度是Hard! 二.我的解答 看到这个题目,我首先想到的是dp,用dp[i][j]表示第i行第j列元素向 ...

  3. 求解最大矩形面积 — leetcode 85. Maximal Rectangle

    之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...

  4. 85. Maximal Rectangle (Graph; Stack, DP)

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  5. 【LeetCode】85. Maximal Rectangle

    Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...

  6. 【leetcode】85. Maximal Rectangle(单调栈)

    Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...

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

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

  8. LeetCode OJ 85. Maximal Rectangle

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...

  9. 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 ...

随机推荐

  1. 20175212童皓桢 实验三敏捷开发与XP实践实验报告

    20175212童皓桢 实验三敏捷开发与XP实践实验报告 实验内容 XP基础 XP核心实践 相关工具 实验步骤 一.Code菜单功能的研究 Move Line/statement Down/Up:将某 ...

  2. ERROR: Command "python setup.py egg_info" python-nss

    [root@localhost ~]# pip install python-nss DEPRECATION: Python 2.7 will reach the end of its life on ...

  3. I/O检测介绍

    I/O性能监测可总结如下:* 任何时间出现CPU等待IO,说明磁盘超载.* 计算出你的磁盘可维持的IOPS值.* 判定你的应用是属于随机磁盘访问型还是有序型.* 通过对比等待时间和服务时间即可判断磁盘 ...

  4. MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)

    背景说明 需求:MySQL树形结构, 根据指定的节点,获取其下属的所有子节点(包含路径上的枝干节点和叶子节点) 枝干节点:如果一个节点下还有子节点,则为枝干节点. 叶子节点:如果一个节点下不再有子节点 ...

  5. java:LeakFilling (Mybatis)

    1.实体类属性与数据库中字段名字不一样时,或者起别名时: TbOrderMapper.xml配置文件中,配置resultMap标签: 其它相同的标签也需要配,否则查询不出来对应数据. 2.一对一关联: ...

  6. 如何获取当前包名与activitity&&抓log

    若hi1:获取当前包名以及Activity (1)adb shell dumpsys activity | find "mFocusedActivity" (2)adb shell ...

  7. C++类大小的计算

    这里记录一下怎么计算类对象的大小. 大概总结下,类的大小需要考虑以下内容: 非静态成员变量大小 数据对齐到多少位 有无虚函数(即需不需要指向虚函数表的指针,如果考虑继承的情况,则还需要看继承了多少个指 ...

  8. Express全系列教程之(十一):渲染ejs模板引擎

    一.简介 相比于jade模板引擎,ejs对原HTML语言就未作出结构上的改变,只不过在其交互数据方面做出了些许修改,相比于jade更加简单易用.因此其学习成本是很低的.您也可参考ejs官网:https ...

  9. 论文翻译:HetConv-Heterogeneous Kernel-Based Convolutions for Deep CNNs

    Abstract 我们提出了一种新颖的深度学习架构,其中卷积操作利用了异构内核.与标准卷积运算相比,所提出的HetConv(基于异构内核的卷积)减少了计算(FLOPs)和参数的数量,同时仍保持表示效率 ...

  10. vue --》路由query 编程式导航传值与监听

    1.首先在一个页面设置一个按钮,用于路由跳转 <template> <div> <button @click="handleToRouter"> ...