Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.

Example:

Input: 

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0 Output: 4 思路是DP, 3种做法, 通用的T: O(m*n) , S: O(m*n) 和只针对部分情况可以use 滚动数组来reduce space成为O(n).
A[i][j] = min(A[i-1][j-1], left[i][j-1], up[i-1][j]) + 1 为边长 i, j > 0 滚动数组
A[i][j] = min(A[i-1][j-1], A[i][j-1], A[i-1][j]) + 1 为边长  i, j > 0
A[i][j] = min(A[i%2-1][j-1], A[i%2][j-1], A[i%2-1][j]) + 1 为边长  i, j > 0

1. Constraints
1) size >=[0*0]
2) element will be "1" or "0" # note it will be integer or string 2. Ideas DP T: O(m*n) S: O(n) optimal
1) edge case, empty, m == 1 or n == 1
2) left, up , ans, init
3)
A[i][j] = min(A[i-1][j-1], left[i][j-1], up[i-1][j]) + 1
4) return res*res 3. codes 1) use left, up , and ans T: O(m*n) S: O(m*n)
 class Solution:
def maxSquare(self, matrix):
if not matrix: return 0
m, n = len(matrix), len(matrix[0])
left, up, ans, res = [[0]*n for _ in range(m)], [[0]*n for _ in range(m)], [[0]*n for _ in range(m)], 0
for i in range(m):
for j in range(n):
if matrix[i][j] == "":
res = 1 # edge case when m == 1 or n == 1
if j == 0:
left[i][j] = ans[i][j] = 1
if i == 0:
up[i][j] = ans[i][j] = 1
if i >0 and j > 0:
left[i][j] = left[i][j-1] + 1
up[i][j] = up[i-1][j] + 1
for i in range(1, m):
for j in range(1, n):
if matrix[i][j] == "":
ans[i][j] = min(ans[i-1][j-1], left[i][j-1], up[i-1][j])+1
res = max(res, ans[i][j])
return res*res

3.2) skip left and up, just use f array

T: O(m*n)    S: O(m*n)
class Solution:
def maxSquare(self, matrix):
if not matrix or not matrix[0]: return 0
m, n = len(matrix), len(matrix[0])
f, ans = [[0] * n for _ in range(m)], 0
# initial f
for i in range(m):
if matrix[i][0] == "":
f[i][0] = 1
ans = 1 # edge case when only edge is 1
for j in range(n):
if matrix[0][j] == "":
f[0][j] = 1
ans = 1
for i in range(1, m):
for j in range(1, n):
if matrix[i][j] == "":
f[i][j] = min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1
ans = max(ans, f[i][j])
return ans * ans

3.2.1) 将初始化都放在f赋值的两个for loop中:

T: O(m*n)    S: O(m*n)
class Solution:
def maxSquare(self, matrix):
if not matrix or not matrix[0]: return 0
m, n = len(matrix), len(matrix[0])
f, ans = [[0] * n for _ in range(m)], 0
for i in range(m):
for j in range(n):
if matrix[i][j] == "":
if i == 0 or j == 0:
f[i][j] = 1
else:
f[i][j] = min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1
ans = max(ans, f[i][j])
return ans * ans

3.3) 滚动数组,   T: O(m*n),    S: O(n)

class Solution:
def maxSquare(self, matrix):
if not matrix or not matrix[0]: return 0
m, n = len(matrix), len(matrix[0])
f, ans = [[0] * n for _ in range(2)], 0
for i in range(m):
for j in range(n):
if matrix[i][j] == "":
if i == 0 or j == 0:
f[i % 2][j] = 1
else:
f[i % 2][j] = min(f[(i - 1) % 2][j], f[i % 2][j - 1], f[(i - 1) % 2][j - 1]) + 1
ans = max(ans, f[i % 2][j])
else:
f[i % 2][j] = 0 #Note: must notice when using rolling array, need to initial
return ans * ans

4. Test cases

1) edge case

2)

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
 

[LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming的更多相关文章

  1. [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  2. [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  3. [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

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

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

  5. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. [LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  7. [LintCode] 77. Longest common subsequences_ Medium tag: Dynamic Programming

    Given two strings, find the longest common subsequence (LCS). Example Example 1: Input: "ABCD&q ...

  8. [LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  9. (medium)LeetCode 221.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. JS基础---->javascript的基础(一)

    记录一些javascript的基础知识.只是一起走过一段路而已,何必把怀念弄的比经过还长. javascript的基础 一.在检测一个引用类型值和 Object 构造函数时, instanceof 操 ...

  2. SVN —— 如何设置代理

    如果在使用SVN下载外网的资源时,出现这样的提示:No such host is known. 或者 不知道这样的主机,可能是机器网络的问题. 如果浏览器能够正常访问外网,那应该是网络设置了代理的问题 ...

  3. C和C++书籍推荐

    http://bestcbooks.com/recommend/most-influential-book/ http://www.ruanyifeng.com/blog/2011/09/c_prog ...

  4. 电子邮件 -- 图解TCP_IP_第5版

    图解TCP_IP_第5版 作者: [日]竹下隆史 / [日]村山公保 / [日]荒井透 / [日]苅田幸雄 出版社: 人民邮电出版社原作名: マスタリングTCP/IP 入門編 第5版译者: 乌尼日其其 ...

  5. Linux系统启动内幕

    经过对Linux系统有了一定了解和熟悉后,想对其更深层次的东西做进一步探究.这当中就包括系统的启动流程.文件系统的组成结构.基于动态库和静态库的程序在执行时的异同.协议栈的架构和原理.驱动程序的机制等 ...

  6. C程序设计语言习题(3-5)

    编写函数itob(n,s,b),将整数n转换为以b为底的数,并将转换结果以字符的形式保存到字符串s中.e.g.itob(n,s,16)把整数n格式化为十六进制整数保存在s中. #include< ...

  7. 【咸鱼教程】基于系统时间的计时器DateTimer(不受FPS影响)

    教程目录一 计时器简介二 计时器实现三 Demo下载 一 计时器简介在手机上跑游戏时,可能由于运动物体过多,导致帧频太低,计时不准确.比如一些倒计时的游戏,可能倒计时30s,变成了35s.比如ipho ...

  8. eclipse打断点只进入class文件中的解决办法

    内容来源 https://www.cnblogs.com/scode2/p/8671908.html#undefined 是由于对应的Java类跟编译后的class文件,没有关联上, 解决办法: 在打 ...

  9. java读取写入oracle的blob字段工具类

    import com.hzunitech.fxgk.sys.model.UtFileData;import com.jfinal.kit.PathKit;import com.jfinal.plugi ...

  10. Java 8新增的Lambda表达式

    一. 表达式入门 Lambda表达式支持将代码块作为方法参数,lambda表达式允许使用更简洁的代码来创建只有一个抽象方法的接口(这种接口被称为函数式接口)的实例,相当于一个匿名的方法. 1.1 La ...