[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 ...
随机推荐
- Esper学习之九:EPL语法(五)
本篇的内容主要包括了Subquery(也就是子查询)和Join,内容不少,但是不难,基本上和sql差不太多. 1.Subquery EPL里的Subquery和sql的类似,是否比sql的用法更多我不 ...
- 【大数据系列】hadoop2.0中的jobtracker和tasktracker哪里去了
低版本的hadoop下MapReduce处理流程 1.首先用户程序(JobClient)提交了一个job,job的信息会发送到Job Tracker,Job Tracker是Map-reduce框架的 ...
- jQuery Sizzle选择器(二)
自己开始尝试读Sizzle源码. 1.Sizzle同过自执行函数的方式为自己创建了一个独立的作用域,它可以不依赖于jQuery的大环境而独立存在.因此它可以被应用到其它js库中.实现如下:(fun ...
- linux下 php 安装mysql的扩展模块
1.安装mysql-devel包 [root@DBproxy ~]# yum install mysql-devel 注:该包必须在编译php之前安装好,否则在安装php的mysql扩展模块是会碰到各 ...
- LeetCode 48 Rotate Image(2D图像旋转问题)
题目链接: https://leetcode.com/problems/rotate-image/?tab=Description Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...
- sencha touch carousel 扩展 CardList 可绑定data/store
扩展代码: /* *扩展carousel *通过data,tpl,store配置数据 */ Ext.define('ux.CardList', { extend: 'Ext.carousel.Caro ...
- Linux下重启mysql的时候出现 start: Job failed to start
mysql进程自己退出了,使用如下指令确认mysql进程不在了. ps -ef | grep mysql 看不到mysql进程 mysql进程不在,尽快回复服务的想法,就是重启服务 /etc/init ...
- 部署OpenStack问题汇总(二)--openstack dashboard 问题解决方案
在打开dashboard的时候报错: LocationParseError at /admin/ (LocationParseError(...), 'Failed to parse: Failed ...
- 一篇博客搞定redis基础
redis简介 redis 一款高性能key-value数据库,实际上多用作缓存队列或者消息分发(celery),但是最常常被用来做缓存. redis安装 源码安装 $ wget http://dow ...
- 大规模Elasticsearch集群管理心得
转载:http://elasticsearch.cn/article/110 ElasticSearch目前在互联网公司主要用于两种应用场景,其一是用于构建业务的搜索功能模块且多是垂直领域的搜索,数据 ...