maximal-rectangle
题目:牛客网
解题思路:
利用递增栈,可参考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相关推荐
- [LeetCode]Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 【DP】LeetCode 85. Maximal Rectangle
LeetCode 85. Maximal Rectangle Solution1: 一语惊醒梦中人啊,参考链接:https://www.youtube.com/watch?v=2Yk3Avrzauk ...
- leetcode 85. Maximal Rectangle | 85. 最大矩形(单调栈)
题目 https://leetcode.com/problems/maximal-rectangle/ 题解 本题与 leetcode 84. Largest Rectangle in Histogr ...
- LeetCode 笔记系列 18 Maximal Rectangle [学以致用](最大矩形)
leetcode之Largest Rectangle in Histogram 标签: leetcode面试题最大矩形堆栈单调队列 2016-07-30 13:47 1325人阅读 评论(0) 收藏 ...
- 【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 ...
- 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 ...
- Leetcode: Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- Leetcode | Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- LeetCode Maximal Rectangle(dp)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...
- [leetcode]@python 85. Maximal Rectangle
题目链接 https://leetcode.com/problems/maximal-rectangle/ 题目原文 Given a 2D binary matrix filled with 0's ...
最新文章
- golang通过RSA算法生成token,go从配置文件中注入密钥文件,go从文件中读取密钥文件,go RSA算法下token生成与解析;go java token共用
- LeetCode实战:最长回文子串
- 网站出现service unavailable的解决方法
- StratifiedKFold 用法
- Android App 瘦身总结 第一章 图片资源的优化处理
- js操作完的数值赋回html元素,JS的学习
- python科学计算笔记(四)pandas 数据索引与选取
- 79. 单词搜索(dfs)
- 路由cpu负载过高检查
- 高性能JavaScript DOM编程
- pe如何自动加载外置工具_winpe无法加载外置程序解决方法
- virtualbox虚拟机安装win10详细教程
- 试验设计[实验设计]
- c++简单复数计算器
- hyper-v开启与关闭
- 微软发布的 免费文件恢复工具
- 云服务器的回收站在哪个文件夹,云主机无法从回收站删除解决办法
- java网课|Scanner
- 菜刀 mysql_chopper菜刀一句话操作mysql数据库乱码问题脚本安全 -电脑资料
- Python学习笔记002-安装SQL Server2016(和操作系统般配^-^)