[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 ...
随机推荐
- Android开发进阶从小工到专家之性能优化
- Android M App休眠 (adb shell dumpsys usagestats)
App休眠 在 Marshmallow 系统,Google 宣布了一个新的功能叫 App 休眠.App 休眠会阻止那些不 常用的 App(几天没有用过的 App)连接网络或者是运行任何程序直至设备充电 ...
- cmake openssl ios
1 下载源代码 git clone https://github.com/pol51/OpenSSL-CMake.git cd OpenSSL-CMake mkdir build && ...
- Elasticsearch学习之查询去重
1. 实现查询去重.分页,例如:实现依据qid去重,createTime排序,命令行为: GET /nb_luban_answer/_search { "query": { &qu ...
- 配置Mac漂亮的Shell--Iterm2+OhMyZSH+Agnoster
安装包管理器 首先当然是解决包管理的问题,Mac下面是Homebrew的天下了 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubuserco ...
- Android服务开发——WebService
我在学习Android开发过程中遇到的第一个疑问就是Android客户端是怎么跟服务器数据库进行交互的呢?这个问题是我当初初次接触Android时所困扰我的一个很大的问题,直到几年前的一天,我突然想到 ...
- WIN7开启wifi热点
1.首先,先确定自己的笔记本网卡支持“启动承载网络”的功能,使用管理员运行cmd命令.启用管理员运行CMD的方法Windows-所有程序-附件-运行(右键,以管理员身份运行)在弹出的CMD窗口里面敲击 ...
- iOS 循环引用 委托 (实例说明)
如何避免循环引用造成的内存泄漏呢: 以delegate模式为例(viewcontroller和view之间就是代理模式,viewcontroller有view的使用权,viewcontroller同时 ...
- AJAX之三种数据传输格式详解
一.HTML HTML由一些普通文本组成.如果服务器通过XMLHTTPRequest发送HTML,文本将存储在responseText属性中. 从服务器端发送的HTML的代码在浏览器端不需要用Java ...
- 浅析JSONP
什么是JSONP? JSONP是JSON with Padding的略称.它是一个非官方的协议,它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式 ...