最大正方形 · Maximal Square
[抄题]:
在一个二维01矩阵中找到全为1的最大正方形
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
返回 4
[暴力解法]:
时间分析:
空间分析:i j 中保留一排,用指针数组来优化空间存储
[思维问题]:
[一句话思路]:
棋盘类dp也是用扩展,不过是从右下角开始扩展 最大扩展中的最小值,没见过
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 第0列初始化完成时,j 从1 开始。第一次发现初始化会对后续计算结果产生影响
- 某点的最大扩展f是收到其右下角三个点的计算得出的最大扩展f共同制约的,要看图理解
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
i or j中的一个只有2种状态,所以可以mod2
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
格子类dp 属于坐标型
[关键模板化代码]:
f[i % 2][0] = matrix[i][0];
只有状态数组f要mod2
[其他解法]:
[Follow Up]:
空间优化
[LC给出的题目变变变]:
85. Maximal Rectangle 还是dp但是图形分析更复杂
[代码风格] :
public int maximalSquare(char[][] matrix) {
//state
//corner case
int n = matrix.length;
int m = matrix[0].length;
int[][] f = new int[2][m];
int ans = 0;
if (n == 0) {
return 0;
}
//initialize for i = 0, j >= 1
for (int i = 0; i < n; i++) {
if (matrix[i][0] == '1')
{f[i % 2][0] = 1;
ans = Math.max(f[i % 2][0], ans);}
for (int j = 1; j < m; j++) {
//if row is not 0
if (i > 0) {
//if matrix[i][j] exists
if (matrix[i][j] == '1') {
//+1
f[i % 2][j] = 1 + Math.min(f[(i - 1) % 2][j],Math.min(f[i % 2][j - 1], f[(i - 1) % 2][j - 1]));
}
else {
f[i % 2][j] = 0;
}
}else {
//if row is 0
if (matrix[i][0] == '1')
f[i % 2][j] = 1;
}
ans = Math.max(f[i % 2][j], ans);
}
}
//result
return ans * ans;
最大正方形 · Maximal Square的更多相关文章
- [Swift]LeetCode221. 最大正方形 | Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- [LintCode] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 【动态规划】leetcode - Maximal Square
称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- [LeetCode] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
随机推荐
- 本地代码同步到github
1 设置 ssh 公钥信息 首先你要确保 github 账号设置了ssh 公钥信息.如果没有的话可以按照下面的方式设置: 前往 github 网站的 account settings, 依次点击 Se ...
- CentOS6下源码安装mysql-5.6.25
1.1.系统环境检查 1)检查系统版本 mkdir -p /server/tools/ cd /server/tools/ cat /etc/redhat-release 2)配置域名解析 vim / ...
- PHP write byte array to file
/********************************************************************************* * PHP write byte ...
- .NET 命令行参数包含应用程序路径吗?
如果你关注过命令行参数,也许发现有时你会在命令行参数的第一个参数中中看到应用程序的路径,有时又不会.那么什么情况下有路径呢? 其实是否有路径只是取决于获取命令行参数的时候用的是什么方法.而这是 Win ...
- CH1807 Necklace
题意 背景 有一天,袁☆同学绵了一条价值连城宝石项链,但是,一个严重的问题是,他竟然忘记了项链的主人是谁!在得知此事后,很多人向☆同学发来了很多邮件,都说项链是自己的,要求他归还(显然其中最多只有一个 ...
- macOS --- 配置基于域名的虚拟主机
在终端运行 sudo vi /Applications/XAMPP/xamppfiles/etc/httpd.conf,打开apache配置文件. 在httpd.conf中找到"#Inclu ...
- Oracle基本操作命令
一.Oracle监听命令 1.启动监听 lsnrctl start 五.Oracle 查看表空间的大小及使用情况sql语句 --1.查看表空间的名称及大小 SELECT t.tablespace_na ...
- bzoj2337 XOR和路径
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2337 首先:因为是异或和,所以可以考虑每一位考虑. 就在每一位上求一下该位是1的概率,乘以1 ...
- 增加MyEclips 智能提示功能
一般默认情况下,Eclipse ,MyEclipse 的代码提示功能是比Microsoft Visual Studio的差很多的,主要是Eclipse ,MyEclipse本身有很多选项是默认关闭的, ...
- 02 - Unit010:关联映射
关联映射 什么是? 数据库中有关联关系的表,通过实体对象引用的方式体现出来,叫关联映射. 为什么? 将多表的记录封装成实体对象. 何时用? 对数据库中的表进行多表查询时. 怎么用? cn_user-- ...