最大正方形 · 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 ...
随机推荐
- ubuntu16切换hosts软件安装
之前用ubuntu切换hosts以为没有软件可用,直接用cp来替换hosts文件,今天网上搜了一下发现一个软件和window上用的切hosts功能一样,而且可以支持linux,mac,windows. ...
- HDU 3667
http://acm.hdu.edu.cn/showproblem.php?pid=3667 最小费用最大流 本题流量和费用不是线性关系,fee=a*flow*flow,所以常规套模板spfa无法得到 ...
- Ubuntu下MySQL数据库文件 物理迁移后 出现的问题
参考资料: https://www.cnblogs.com/advocate/archive/2013/11/19/3431606.html 本文要解决的一个问题是数据库文件进行物理迁移时遇到的问题. ...
- ImportError: No module named 'serial'
/******************************************************************************** * ImportError: No ...
- 使用 Content-Encoding: br 替换 Content-Encoding: gzip
今天在测试一个web 框架的时候无意发现框架运行是响应头时有一个 Content-Encoding: br 发现是一个gzip 算法的替代,同时压缩比很不错 浏览器兼容性如下: nginx 的配置参考 ...
- hadoop YARN配置参数剖析—MapReduce相关参数
MapReduce相关配置参数分为两部分,分别是JobHistory Server和应用程序参数,Job History可运行在一个独立节点上,而应用程序参数则可存放在mapred-site.xml中 ...
- Java中return的语句
1.return语句的作用:a.返回一个值,这个值可以是任意类型.b.使程序返回到操作系统(即终止程序)2.java中对于一个函数,不论有没有返回值类型,都可以带有return 语句.但是区别在于,r ...
- vim配置之tagbar
vimConfig/plugin/tagbar-setting.vim let g:tagbar_width=4 map <F12> :TagbarToggle<CR> map ...
- URL 收录
http://www.cnblogs.com/Olive116/p/3426957.html 地图控件 http://www.cnblogs.com/tugenhua0707/ 前端
- mysql-12序列使用
mysql序列是一组整数:1,2,3....,由于一张数据表只能有一个字段自增主键,如果你想实现其他字段自动增加,就可以使用mysql序列来实现. 使用auto_increment来定义列 drop ...