85. 最大矩形

85. 最大矩形

class Solution {
   
    public int maximalRectangle(char[][] matrix) {
   
        int m = matrix.length, n = matrix[0].length;
        int[] heights = new int[n];
        int res = 0;

        for(int i = 0; i < m; i++){
   
            for(int j = 0; j < n; j++){
   
                if(matrix[i][j] == '1') heights[j]++;
                else heights[j] = 0;
            }
            res = Math.max(res, largestRectangleArea(heights));
        }

        return res;
    }

    int largestRectangleArea(int[] heights) {
   
        int len = heights.length + 2;
        int[] newHeight = new int[len];
        for(int i = 1; i < len - 1; i++) newHeight[i] = heights[i - 1];

        Deque<Integer> stack = new ArrayDeque<>();
        int res = 0;

        for(int i = 0; i < len; i++){
   
            while(!stack.isEmpty() && newHeight[i] < newHeight[stack.peek()]){
   
                int pop = stack.pop();
                int w = i - stack.peek() - 1;
                int h = newHeight[pop];
                int area = h * w;
                res = Math.max(res, area);
            }
            stack.push(i);
        }

        return res;
    }
}

相关推荐

  1. 85. 矩形

    2023-12-10 03:58:01       62 阅读
  2. 算法37:矩形(力扣8485题)---单调栈

    2023-12-10 03:58:01       55 阅读
  3. LeetCode 84. 柱状图中矩形

    2023-12-10 03:58:01       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-10 03:58:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-10 03:58:01       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-10 03:58:01       82 阅读
  4. Python语言-面向对象

    2023-12-10 03:58:01       91 阅读

热门阅读

  1. 力扣labuladong一刷day31天二叉树

    2023-12-10 03:58:01       54 阅读
  2. 在Go中使用循环时使用Break和Continue语句

    2023-12-10 03:58:01       58 阅读
  3. 汽车网络安全--ISO\SAE 21434解析(一)

    2023-12-10 03:58:01       59 阅读
  4. Environment Variables Used by GPUDirect Storage

    2023-12-10 03:58:01       59 阅读
  5. 解释 Git 的基本概念和使用方式。

    2023-12-10 03:58:01       38 阅读
  6. 12.5每日一题(备战蓝桥杯小数运算、拆位练习)

    2023-12-10 03:58:01       37 阅读
  7. 【Spring篇】切点表达式语法规范

    2023-12-10 03:58:01       43 阅读
  8. 服务器数据损坏了有办法修复吗 ?

    2023-12-10 03:58:01       56 阅读
  9. [leetcode 双指针]

    2023-12-10 03:58:01       47 阅读
  10. 力扣labuladong——一刷day67

    2023-12-10 03:58:01       60 阅读
  11. 发送、接收消息,界面不及时刷新

    2023-12-10 03:58:01       59 阅读
  12. Linux 基础知识整理(五)

    2023-12-10 03:58:01       53 阅读
  13. linux中slab与slub的实现区别

    2023-12-10 03:58:01       50 阅读