[LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming
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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- [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 ...
- [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 ...
- [LintCode] 77. Longest common subsequences_ Medium tag: Dynamic Programming
Given two strings, find the longest common subsequence (LCS). Example Example 1: Input: "ABCD&q ...
- [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 ...
- (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 ...
随机推荐
- Oracle —— 如何执行SQL文件
在Command模式下(笔者使用的是 PL/SQL Comand Window),输入 @文件路径\文件名 如: @D:\ORA_SQL\INSERT_SQL.sql
- [原]openstack-kilo--issue(二) openstack auth error
/** 系统环境:redhat7.2 repo:163 openstack version : kilo author: lihaibo 本博客已经添加"打赏"功能,"打 ...
- Robotium源码解读-native控件/webview元素的获取和操作
目前比较有名的Uitest框架有Uiautomator/Robotium/Appium,由于一直对webview元素的获取和操作比较好奇,另外Robotium代码量也不是很大,因此打算学习一下. 一. ...
- RDMA卡的检测方法
1. udaddy This script covers RDMA_CM UD connections. (It establishes a set of unreliable RDMA datagr ...
- iOS - Charles抓包数据
一.Charles Charles破解版下载地址点我 1.1 Charles主要的功能 .截取Http.Https网络请求内容 .支持修改网络请求参数,方便调试 .支持网络请求的截取 并动态修改 1. ...
- python基础-动态加载lazy_import(利用__import__)
看了一天动态加载,普遍有这么几种方法,总结一下,由简入深,本文仅对查到的栗子们做个引用……省去你们大把查资料的时间= = 主要思想:把模块(文件)名.类名.方法名当成了变量 然后利用__import_ ...
- linux 命令行常用快捷键
linux命令行常用快捷键,区别于vim编辑器快捷键.熟练掌握下面的快捷键可提高操作linux的工作效率.当然最重要的是可以装屌. 1.移动光标快捷键Ctrl+a光标回到命令行首* Ctrl+e光标回 ...
- jquery 设置style:display 其实很方便的哦
("#id").css('display','none'); $("#id").css('display','block'); 或 $("#id&qu ...
- HTML_css选择器
第二种增加css样式的方法,可以在head中增加style标签,style中通过选择器定位标签增加css样式 CSS选择器分为六种: 1.id选择器 2.class选择器 3.标签选择器 4. ...
- 2018/04/04 PHP 中的 数组排序问题
简单说一下为什么要总结一下PHP中关于数组排序的问题. 在很多时候我们对于数组要进行排序,但是 PHP 中对于排序已经有了很多封装. 但是某些函数是修改原数组,一部分是返回修改后数组,与其一个个试,不 ...