[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 ...
随机推荐
- Web服务器配置Gzip压缩提升网站性能
前言: HTTP协议上的GZIP编码是一种用来改进WEB应用程序性能的技术.大流量的WEB站点常常使用GZIP压缩技术来让用户感受更快的速度.这一般是指WWW服务器中安装的一个功能,当有人来访问这个服 ...
- Email standards
https://www.fastmail.com/help/technical/standards.html Email structure These RFCs define the way ema ...
- Linux 安装Ruby详解(在线和离线安装)
很多时候我们会发现,真实的生成环境很多都没有外网,只有内网环境,这个时候我们又需要安装Ruby,则不能提供yum命令进行在线安装了,这个时候我们就需要下载安装包进行离线安装.本文主要简单介绍如果离线安 ...
- LeetCode 48 Rotate Image(2D图像旋转问题)
题目链接: https://leetcode.com/problems/rotate-image/?tab=Description Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...
- Sencha Touch 实战开发培训 视频教程 第二期 第三节
2014.4.11晚上8:10分开课. 本节课耗时一小时以上. 本期培训一共八节,前两节免费,后面的课程需要付费才可以观看. 本节内容: 本地储存.扩展按钮控件.微博分享 实现 ...
- rabbitmq在centos 7上的安装
一.安装步骤 参考了官网文档: http://www.rabbitmq.com/install-rpm.html#package-dependencies 这里大概介绍下. rabbitmq-serv ...
- Unity3D Shader落雪效果
Shader "Custom/Snow" { Properties { _MainTex ("Base (RGB)", 2D) = "white&qu ...
- Instrumentation 两种方法 premain Agent
由于jvm内部的限制Instrumentation 只能修改方法体 不能动态添加删除方法(安全第一吧!!!!) Premain 对于使用命令行接口的实现,可以将以下选项添加到命令行来启动代理: -ja ...
- CodeForces 832B Petya and Exam
B. Petya and Exam time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Oracle管理监控之检查数据库和日常维护数据库
linux系统的系统日志一般位于/var/log目录下.linux的系统日志由一个叫syslog的进程管理的,如下日志都是由syslog服务驱动的. /var/log/ messages:记录linu ...