• 面试算法40:矩阵中的最大矩形


    题目

    请在一个由0、1组成的矩阵中找出最大的只包含1的矩形并输出它的面积。例如,在图6.6的矩阵中,最大的只包含1的矩阵如阴影部分所示,它的面积是6。
    在这里插入图片描述

    分析

    直方图是由排列在同一基线上的相邻柱子组成的图形。由于题目要求矩形中只包含数字1,因此将矩阵中上下相邻的值为1的格子看成直方图中的柱子。如果分别以图6.6中的矩阵的每行为基线,就可以得到4个由数字1的格子组成的直方图,如图6.7所示。
    在这里插入图片描述

    在将矩阵转换成多个直方图之后,就可以计算并比较每个直方图的最大矩形面积,所有直方图中的最大矩形也是整个矩阵中的最大矩形。例如,图6.7(c)的直方图中的最大矩形(阴影部分)也是图6.6中矩阵的最大矩形。

    public class Test {
        public static void main(String[] args) {
            char[][] matrix = {
                {'1', '0', '1', '0', '0' },
                {'0', '0', '1', '1', '1' },
                {'1', '1', '1', '1', '1' },
                {'1', '0', '0', '1', '0' }
            };
            int result = maximalRectangle(matrix);
            System.out.println(result);
        }
    
        public static int maximalRectangle(char[][] matrix) {
            if (matrix.length == 0 || matrix[0].length == 0) {
                return 0;
            }
    
            int[] heights = new int[matrix[0].length];
            int maxArea = 0;
            for (char[] row : matrix) {
                for (int i = 0; i < row.length; i++) {
                    if (row[i] == '0') {
                        heights[i] = 0;
                    }
                    else {
                        heights[i]++;
                    }
                }
    
                maxArea = Math.max(maxArea, largestRectangleArea(heights));
            }
    
            return maxArea;
        }
    
        public static int largestRectangleArea(int[] heights) {
            Stack<Integer> stack = new Stack<>();
            stack.push(-1);
    
            int maxArea = 0;
            for (int i = 0; i < heights.length; i++) {
                while (stack.peek() != -1 && heights[stack.peek()] >= heights[i]) {
                    int height = heights[stack.pop()];
                    int width = i - stack.peek() - 1;
                    maxArea = Math.max(maxArea, height * width);
                }
    
                stack.push(i);
            }
    
            while (stack.peek() != -1) {
                int height = heights[stack.pop()];
                int width = heights.length - stack.peek() - 1;
                maxArea = Math.max(maxArea, height * width);
            }
    
            return maxArea;
        }
    }
    
    • 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
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
  • 相关阅读:
    python实现视频剪切与拼接
    menuconfig 图形化配置原理说明三
    简单聊聊 分布式
    远程VPN登录,IPsec,VPN,win10
    Unity做360度全景预览
    Leveldb学习笔记:leveldb的使用与原理探究
    通过后台系统添加一段div,在div中写一个<style></style>标签来修改div外面的元素的深层元素的样式
    python基于PHP+MySQL的家装设计平台管理系统
    【Java】Collection集合&泛型
    C#-单例模式
  • 原文地址:https://blog.csdn.net/GoGleTech/article/details/134014122