题目:牛客网

解题思路:

利用递增栈,可参考https://blog.csdn.net/qq_17550379/article/details/85093224

这种解法显然很慢,我们有一种更好的思路就是通过递增栈。所谓的递增栈,就是栈中只存放递增序列。

我们首先将2加入到栈中,我们接着访问1,我们发现1比栈顶元素2小,所以我们将栈顶元素2弹出,并且记录此时的面积2。我们发现栈已经空了,所以我们要接着压栈。

接着我们通过不断遍历找到第二个递增栈。

我们接着访问2,我们发现此时2比栈顶元素6小,所以我们弹出栈顶元素6,并且记录此时的面积6*1=6。

此时栈中还有元素,我们发现此时2依旧比栈顶元素5小,所以我们需要将栈顶元素5弹出,并且我们记录此时出栈元素构成的最大面积5*2=10。

我们发现此时的2比栈顶元素大了,我们就将2压入栈中,接着将3压入栈中。此时遍历结束,我们发现栈不为空,所以我们需要进行出栈操作,出栈的同时记录出栈元素构成的最大面积即可。

import java.util.Stack;
public class Solution {public int maximalRectangle(char[][] matrix) {//若矩阵为null,或长度为0,或宽度为0,则直接返回0if (matrix == null || matrix.length == 0 || matrix[0].length == 0)return 0;int row = matrix.length;int col = matrix[0].length;int max = 0;int[] h = new int[col];for (int i = 0; i < row; i++) {Stack<Integer> stack = new Stack<Integer>();stack.push(-1);for (int j = 0; j < col; j++) {if (matrix[i][j] == '1')h[j] += 1;elseh[j] = 0;}for (int j = 0; j < col; j++) {while (stack.peek() != -1 && h[j] < h[stack.peek()]) {int top = stack.pop();max = Math.max(max, (j - 1 - stack.peek()) * h[top]);}stack.push(j);}while (stack.peek() != -1) {int top = stack.pop();max = Math.max(max, (col - 1 - stack.peek()) * h[top]);}}return max;}
}

maximal-rectangle相关推荐

  1. [LeetCode]Maximal Rectangle

    Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...

  2. 【DP】LeetCode 85. Maximal Rectangle

    LeetCode 85. Maximal Rectangle Solution1: 一语惊醒梦中人啊,参考链接:https://www.youtube.com/watch?v=2Yk3Avrzauk ...

  3. leetcode 85. Maximal Rectangle | 85. 最大矩形(单调栈)

    题目 https://leetcode.com/problems/maximal-rectangle/ 题解 本题与 leetcode 84. Largest Rectangle in Histogr ...

  4. LeetCode 笔记系列 18 Maximal Rectangle [学以致用](最大矩形)

    leetcode之Largest Rectangle in Histogram 标签: leetcode面试题最大矩形堆栈单调队列 2016-07-30 13:47 1325人阅读 评论(0) 收藏  ...

  5. 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵

    1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  6. LeetCode 85. Maximal Rectangle --python,java解法

    题目地址: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  7. Leetcode: Maximal Rectangle

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  8. Leetcode | Maximal Rectangle

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  9. LeetCode Maximal Rectangle(dp)

     Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...

  10. [leetcode]@python 85. Maximal Rectangle

    题目链接 https://leetcode.com/problems/maximal-rectangle/ 题目原文 Given a 2D binary matrix filled with 0's ...

最新文章

  1. golang通过RSA算法生成token,go从配置文件中注入密钥文件,go从文件中读取密钥文件,go RSA算法下token生成与解析;go java token共用
  2. LeetCode实战:最长回文子串
  3. 网站出现service unavailable的解决方法
  4. StratifiedKFold 用法
  5. Android App 瘦身总结 第一章 图片资源的优化处理
  6. js操作完的数值赋回html元素,JS的学习
  7. python科学计算笔记(四)pandas 数据索引与选取
  8. 79. 单词搜索(dfs)
  9. 路由cpu负载过高检查
  10. 高性能JavaScript DOM编程
  11. pe如何自动加载外置工具_winpe无法加载外置程序解决方法
  12. virtualbox虚拟机安装win10详细教程
  13. 试验设计[实验设计]
  14. c++简单复数计算器
  15. hyper-v开启与关闭
  16. 微软发布的 免费文件恢复工具
  17. 云服务器的回收站在哪个文件夹,云主机无法从回收站删除解决办法
  18. java网课|Scanner
  19. 菜刀 mysql_chopper菜刀一句话操作mysql数据库乱码问题脚本安全 -电脑资料
  20. Python学习笔记002-安装SQL Server2016(和操作系统般配^-^)

热门文章

  1. 数学专业的数学与计算机专业的数学的比较(转)
  2. hypixel服务器显示无法登录,我的世界hypixel服务器盗版可以登录?
  3. http://www.cvvision.cn/2888.html
  4. Python基于周立功盒子的二次开发的封装和调用
  5. JAVA8实战 -- Lamdba表达式
  6. 三分钟搞明白Java内存分布,再也不怕了!!!
  7. v-for错误:应为数组元素析构模式
  8. Kanzi 的开机优化
  9. 阿里云企业认证时不要让员工做个人认证的原因
  10. Android 设置向导启动分析