MaximalRectangle
1.问题描述
给定一个由'0'和'1'组成的2D空间,找到这个空间中只包含1的最大矩阵
2.解题思路
将此问题转换为求只包含'1'的左边界和右边界问题
3.程序源码
public int maximalRectangle(char[][] matrix) {
if(matrix==null || matrix.length==0 || matrix[0].length==0)
return 0;
int max = 0;
int rows = matrix.length; //行数
int cols = matrix[0].length;//列数
int[] height = new int[cols];//第i行第j列直方图的高
int[] left = new int[cols]; //左边界(与高度相关进行判断)
int[] right = new int[cols]; //右边界
for(int i=0; i<cols; i++){
right[i] = cols;
}
for(int i=0; i<rows; i++){//第i层
int curLeft = 0;
int curRight = cols; //当前左边界和右边界
//更新左边界
for(int j=0; j<cols; j++){
if(matrix[i][j] == '1'){
height[j]++;
left[j] = Math.max(curLeft, left[j]);
}else{
height[j] = 0;
curLeft = j+1;
left[j] = 0;
}
}
for(int j=cols-1; j>=0; j--){ //更新右边界
if(matrix[i][j]=='1'){
right[j] = Math.min(curRight, right[j]); //很重要的一步
}else{
right[j] = cols;
curRight = j;
}
//max = Math.max(max, (right[j]-left[j])*height[j]);
}
for(int k=0;k<cols;k++){
if(max<(right[k]-left[k])*height[k]){
max=(right[k]-left[k])*height[k];
}
}
}
return max;
}
MaximalRectangle相关推荐
- LeetCode——maximal-rectangle
Question Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ...
- maximal-rectangle
题目:牛客网 解题思路: 利用递增栈,可参考https://blog.csdn.net/qq_17550379/article/details/85093224 这种解法显然很慢,我们有一种更好的思路 ...
- java基础编程题(2)
1.给定一个二叉树,找出其最大深度. 注:二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. /*** Definition for a binary tree node.* public cl ...
- 语音计算矩形面积_LeetCode85-最大矩形
今天在制作书签的时候 突然想到了一个问题 如果要送给未来的女朋友一个书签 上面该写些什么话 哈哈哈哈哈哈哈哈哈 The Spring is coming! 想了一会儿,觉得这句话最合适 To xxx: ...
- 【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 ...
- 85. Maximal Rectangel
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...
最新文章
- 上海交大与《Science》发布125个最具挑战的科学问题
- sscanf()的用法和例子
- MySQL_控制台操作_01
- 线程Java的两种方式_多线程(java和guava两种方式):
- mfs分布式存储+master端高可用
- PyCharm设置自动换行
- 转换背景色 html,html – CSS转换:淡化背景颜色,重置后
- 细分shared_ptr智能指针在各个版本的使用情况
- Introduction to Latent Dirichlet Allocation
- HTML5+CSS期末大作业:个人网站设计——响应式个人简历介绍网页(5页) HTML+CSS+JavaScript
- 自由软件运动与GNU项目
- 联想昭阳E46A不能上网
- 无线AP与无线路由的区别
- 漏洞修复:Often Misused: HTTP Method Override
- 计算机考研怎么给老师发邮件,2015考研复试给导师的邮件怎么写?_跨考网
- 单片机编程软件很简单(17),Keil单片机编程软件之编译、链接
- HTTP协议中的一些概念
- F2FS文件系统架构与原理分析(五)——元数据组织及管理
- Windows10任务计划怎么用
- 字符串转换成整数-----字符串‘123456’转换成数值123456(指针)
热门文章
- 我的世界java版如何加入hypixel_我的世界hypixel服务器怎么组队 hypixel服务器组队方法介绍...
- CF1463-A. Dungeon
- IOS开发大神装逼神器
- MySQL 替换手机号码中间4位为星号显示
- 微信支付要租用服务器吗,切记!使用微信支付一定要打开这些功能!
- iMX6UL配置MCP2515模块(SPI转CAN)——基于迅为iTOP-iMX6UL开发板
- 2013上半年 系统集成项目管理工程师 下午试卷
- 华为鸿蒙系统8月开源,华为正式宣布,鸿蒙系统8月开源,任正非却说:优先选用安卓...
- 题目 1567: 超级玛丽
- KanziStudio应用程序配置详解