最大正方形 · 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 ...
随机推荐
- threejs 通过bufferGeometry处理每一个点的位置和颜色
let positions = new Float32Array(points.length * 3); let colors = new Float32Array(points.length * 3 ...
- 51Nod 1049:最大子段和(dp)
1049 最大子段和 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 N个整数组成的序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+ ...
- Jenkins搭建windows service自动编译发布环境
类库项目(Task)部署 前面搭建了Web站点的环境,类库项目发布不同于站点项目,它只需要将MSBuild编译出来的dll复制到目标服务器上即可,而不需要通过Web Deploy,下面来说一下如何发布 ...
- 使用 Content-Encoding: br 替换 Content-Encoding: gzip
今天在测试一个web 框架的时候无意发现框架运行是响应头时有一个 Content-Encoding: br 发现是一个gzip 算法的替代,同时压缩比很不错 浏览器兼容性如下: nginx 的配置参考 ...
- 无线密码破解----minidwep-gtk的PIN破解方法
使用虚拟机对minidwep-gtk进行PIN破解 用CDLINUX支持8187和3070_30211版.iso系统PJpin码 1.用虚拟机的好处是方便,可以一边破解,一边上网做其他事情. 虚拟机 ...
- windows内存debug技巧
A) c++ memory/heap corrupt debug 技巧 1. catch first exception2. data breakpointVC tell us some addres ...
- Java多线程编程核心技术,第二章,对象和变量并发访问
1,方法内部变量是线程安全的 2,实例变量非线程安全 3,synchronized是锁对象不是锁方法(锁对象是可以访问非synchronized方法,不可访问同个和其他synchronized方法 4 ...
- bzoj2004公交线路
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2004 好美妙的矩阵乘. 思考: 0.在一个序列上.所以考虑dp. 1.p<=10,k& ...
- Python DB
#!/usr/bin/python #_*_ coding:utf-8 _*_ import MySQLdb import time import threading import random fr ...
- redis位图
<?php function frstr($str){ return str_pad($str,8,'0',STR_PAD_LEFT); } $php=''; $p= frstr(decbin( ...