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. CyclicBarrier源码阅读

    一种允许多个线程全部等待彼此都到达某个屏障的同步机制 使用 多个线程并发执行同一个CyclicBarrier实例的await方法时,每个线程执行这个方法后,都会被暂停,只有当最后一个线程执行完awai ...

  2. Java第09次实验(IO流)--实验报告

    0.字节流与二进制文件 我的代码 用DataOutputStream和FileOutputStream将Student对象写入二进制文件student.data package test; impor ...

  3. mysql的双主模式配置

    第一台:192.168.0.160 第二台:192.168.0.170 主从配置,第一步:192.168.0.160 作为主数据库,192.168.0.170作为从数据库,配置如下: ======== ...

  4. Android 夜间模式的实现

    package com.loaderman.daynightdemo; import android.os.Bundle; import android.support.v7.app.AppCompa ...

  5. BCNF/3NF 数据库设计范式简介

    数据库设计有1NF.2NF.3NF.BCNF.4NF.5NF.从左往右,越后面的数据库设计范式冗余度越低. 满足后一个设计范式也必定满足前一个设计范式. 1NF只要求每个属性是不可再分的,基本每个数据 ...

  6. Lombok 学习指南

    转自:https://segmentfault.com/a/1190000020864572 一.Lombok 简介 Lombok 是一款 Java 开发插件,使得 Java 开发者可以通过其定义的一 ...

  7. 第二章 SpringCloud之Eureka-Server服务发现组件

    1.Eureka简介 文档:https://cloud.spring.io/spring-cloud-netflix/spring-cloud-netflix.html ############### ...

  8. LoadRunner参数化使用mysql数据源

    因为默认是没有mysql驱动的,因此需要在网上下载一个mysql驱动 1. 在网上下载一个是MYSQL数据库的ODBC驱动程序:mysql-connector-odbc-3.51.20-win32.e ...

  9. Linux_RHEL_设置网络

    目录 目录 Selinux Iptable NetworkManager 基本网络配置 编辑网卡子接口 ip指令 ifconfig指令 修改主机名 服务端口 Selinux 是Linux的一种权限管理 ...

  10. 用Node开发桌面应用:NW.js和Electron

    NW.js和Electron对比:[http://tangiblejs.com/posts/nw-js-electron-compared] NW.js:[https://nwjs.io/] Elec ...