Blage's Coding Blage's Coding
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
  • 数组

  • 链表

  • 字符串

  • 二叉树

  • 动态规划

  • 深搜回溯

  • 数学贪心

  • 堆栈队列

    • 394.字符串解码
    • 32. 最长有效括号
    • 739. 每日温度
    • 1130. 叶值的最小代价生成树
    • 84. 柱状图中最大的矩形
    • 42.接雨水
    • 85. 最大矩形
      • 1.单调栈
    • 239.滑动窗口最大值
    • 6891. 机器人碰撞
    • 445. 两数相加 II
    • 2762. 不间断子数组
    • 6919. 使数组中的所有元素都等于零
    • 862. 和至少为 K 的最短子数组
    • 1499. 满足不等式的最大值
    • 2856. 删除数对后的最小数组长度
    • 901. 股票价格跨度
    • 2034. 股票价格波动
    • 1488. 避免洪水泛滥
    • 2940. 找到 Alice 和 Bob 可以相遇的建筑
    • 2454. 下一个更大元素 IV
  • 前缀和

  • 算法设计

  • 位运算

  • WA

  • 算法
  • 堆栈队列
phan
2023-06-08
目录

85. 最大矩形

# 85. 最大矩形 (opens new window)

# 1.单调栈

问题转化:将原数组转化为矩形高度数组,定义height[ i ][ j ]为左侧以matrix[ i ][ j ]为起点的最大矩形高度。例如matrix数组【"1","1","1","0","1","1"】对应的高度数组为【1,2,3,0,1,2】。然后按列搜索整个高度矩阵(每一列都可以看作是一个朝向左侧的柱状图),求出以每个元素作为矩形右边界的最大矩阵面积。从而将问题转化为了求出每一列能够勾勒的最大矩形面积,然后返回所有列结果的最大值。(1130. 叶值的最小代价生成树 | Blage's Coding (opens new window))

确定每个点搜索时生成矩形的生成方式也很关键,比如每次都搜索以该点作为左上角顶点的最大矩形。然而即便如此这对于解题依旧没有帮助,因为即使得到前一个顶点的结果,下一个顶点的宽度和高都是在同时变化的,所以矩形问题除了要有固定起始点的思维,还需要有定高的思想。

暴力时间复杂度O(m*m*n*n)缩减为O(m*n)

class Solution {
    public int maximalRectangle(char[][] matrix) {
        int[][] heights=new int[matrix.length][matrix[0].length];
        for(int i=0;i<matrix.length;i++){
            int temp=matrix[i][0]=='1'?1:0;
            heights[i][0]=temp;
            for(int j=1;j<matrix[0].length;j++){
                if(matrix[i][j]=='1'){
                    temp=matrix[i][j-1]=='1'?temp+1:1;
                    heights[i][j]=temp;
                }
                else{
                    temp=0;
                    heights[i][j]=temp;
                }
            }
        }
        //枚举找到每一列元素的最大值
        Deque<Integer> stack=new LinkedList<>();
        int[] upedg=new int[matrix.length];
        int[] downedg=new int[matrix.length];
        int res=0;
        for(int j=0;j<matrix[0].length;j++){
            stack.clear();
            stack.offerFirst(-1);
            for(int i=0;i<heights.length;i++){
                while(stack.peekFirst()!=-1&&heights[i][j]<=heights[stack.peekFirst()][j]) stack.pollFirst();
                upedg[i]=stack.peekFirst();
                stack.offerFirst(i);
            }
            stack.clear();
            stack.offerFirst(heights.length);
            for(int i=heights.length-1;i>=0;i--){
                while(stack.peekFirst()!=heights.length&&heights[i][j]<=heights[stack.peekFirst()][j]) stack.pollFirst();
                downedg[i]=stack.peekFirst();
                stack.offerFirst(i);
            }
            for(int i=0;i<heights.length;i++){
                int temp=heights[i][j]*(downedg[i]-upedg[i]-1);
                res=Math.max(res,temp);
            }
        }
        return res;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
编辑 (opens new window)
#Leetcode#堆栈队列
上次更新: 2023/12/15, 15:49:57
42.接雨水
239.滑动窗口最大值

← 42.接雨水 239.滑动窗口最大值→

Theme by Vdoing | Copyright © 2023-2024 blageCoder
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式