[抄题]:

在一个二维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也是用扩展,不过是从右下角开始扩展 最大扩展中的最小值,没见过

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 第0列初始化完成时,j 从1 开始。第一次发现初始化会对后续计算结果产生影响
  2. 某点的最大扩展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的更多相关文章

  1. [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 ...

  2. 求解最大正方形面积 — leetcode 221. Maximal Square

    本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...

  3. [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 ...

  4. leetcode每日解题思路 221 Maximal Square

    问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...

  5. 【动态规划】leetcode - Maximal Square

    称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...

  6. 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 ...

  7. 【刷题-LeetCode】221. Maximal Square

    Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...

  8. 【LeetCode】221. Maximal Square

    Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...

  9. [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 ...

随机推荐

  1. ROS会议 ROSCon 2017

    ----ROSCon2012-2017----来源链接:https://roscon.ros.org           近三年ROSCon(2015-2017)都会将会议视频录像和文档公开~以下为机 ...

  2. python实现树结构

    树在计算机科学的许多领域中使用,包括操作系统,图形,数据库系统和计算机网络.树数据结构与他们的植物表亲有许多共同之处.树数据结构具有根,分支和叶.自然界中的树和计算机科学中的树之间的区别在于树数据结构 ...

  3. np.ones(N)/N的作用

    在python中导入numpy包 N=5 weights = np.ones(N)/N       //这里就相当于创建了一个数组,且为5个1/5的数组 print "weights&quo ...

  4. BZOJ4977: [[Lydsy1708月赛]跳伞求生(不错的贪心)

    4977: [[Lydsy1708月赛]跳伞求生 Time Limit: 5 Sec  Memory Limit: 256 MBSubmit: 446  Solved: 142[Submit][Sta ...

  5. linux python 安装 nose lapack atlas numpy scipy

    linux python 安装 nose lapack atlas numpy scipy --http://lib.csdn.net/article/python/1262 作者:maple1149 ...

  6. Oracle XE自带数据库创建的过程

    Oracle XE自带数据库如何创建的?XE.sql脚本定义实例究竟是怎样的?阅读下文,您可以找到这些问题的答案. Oracle XE自带数据库是如何创建的呢?这是很多人都提到过的问题,下面就为您详细 ...

  7. ZedGraph控件的使用 --归类(转帖)

    在我们编写程序的时候,有时候是要做一些统计的,为了达到一目了然的效果,饼状图,曲线图,柱状图都是很好的表现统计的直观形式.这个时候,ZedGraph控件给我们带来了极大的方便. 1.下载ZedGrap ...

  8. 【monkeyrunner】monkeyrunner脚本录制和回放

    脚本录制 1.连接你已经打开调试模式的ANDROID设备或模拟器,输入adb devices 2.运行录制脚本.在cmd窗口输入 monkeyrunner recorder.py #recorder. ...

  9. 学习blus老师js(6)--js运动基础

    运动基础 一.匀速运动 运动框架 在开始运动时,关闭已有定时器 把运动和停止隔开(if/else) <!DOCTYPE HTML> <html> <head> &l ...

  10. Chrome经常新标签页打开http://destyy.com/qNHR3u

    经常新标签页打开http://destyy.com/qNHR3u网址. 在历史记录里查询 chrome://history/?q=destyy.com ,发现最早访问是25日10点34分05.貌似那个 ...