• 面试算法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
  • 相关阅读:
    什么是CAP理论?
    数据结构初阶--栈和队列(讲解+类模板实现)
    【速度收藏】17条好用的Python技巧分享(含源代码)
    数据结构与算法-选择&冒泡&快排&计数
    Promethus(一)概览
    搜索店铺列表API 返回值说明
    Linux 的权限
    计算机的基础知识
    【操作系统】第2章 进程与线程
    【Java SE】this引用注意事项
  • 原文地址:https://blog.csdn.net/GoGleTech/article/details/134014122