leetcode-165周赛-1277-统计全为1的正方形子矩阵
题目描述:
自己的提交:
class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
if not matrix:
return 0
m,n = len(matrix),len(matrix[0])
dp = [0] + [0] * m * n
for i in range(m):
for j in range(n):
dp[i*n+j+1] = dp[i*n+j]
layer = min(i,j)
for l in range(layer+1):
flag = True
for i_ in range(i-l,i+1):
if matrix[i_][j-l] == 0:
flag = False
for j_ in range(j-l,j+1):
if matrix[i-l][j_] == 0:
flag = False
if flag == False:
break
dp[i*n+j+1] += 1
return dp[-1]
优化: O(N^2)
class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
n = len(matrix)
m = len(matrix[0])
dp = [[0] * m for _ in range(0, n)]
ret = 0
for i in range(0, n):
for j in range(0, m):
if matrix[i][j] == 0:
continue
dp[i][j] = 1
if i == 0 or j == 0:
ret += dp[i][j]
continue
sub = min(dp[i - 1][j], dp[i][j - 1])
if sub != 0:
if matrix[i - sub][j - sub] == 1:
dp[i][j] = sub + 1
else:
dp[i][j] = sub
ret += dp[i][j]
return ret
再优化:
class Solution(object):
def countSquares(self, A):
R, C = len(A), len(A[0]) dp = [[0] * (C+1) for _ in range(R+1)]
ans = 0
# largest square ending here
for r, row in enumerate(A):
for c, val in enumerate(row):
if val:
dp[r+1][c+1] = min(dp[r][c], dp[r][c+1], dp[r+1][c]) + 1
ans += dp[r+1][c+1]
return ans
leetcode-165周赛-1277-统计全为1的正方形子矩阵的更多相关文章
- PHP算法之统计全为 1 的正方形子矩阵
在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0 输出: 4 来源:力扣( ...
- 51nod 1158 全是1的最大子矩阵
题目链接:51nod 1158 全是1的最大子矩阵 题目分类是单调栈,我这里直接用与解最大子矩阵类似的办法水过了... #include<cstdio> #include<cstri ...
- 51nod1158 全是1的最大子矩阵
跟最大子矩阵差不多O(n3)扫一下.有更优写法?挖坑! #include<cstdio> #include<cstring> #include<cctype> #i ...
- 51Nod 1158 全是1的最大子矩阵 —— 预处理 + 暴力枚举 or 单调栈
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1158 1158 全是1的最大子矩阵 基准时间限制:1 秒 空 ...
- [LeetCode] Count Binary Substrings 统计二进制子字符串
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- leetcode 165
才一周没刷leetcode,手就生了,这个题目不难,但是完全AC还是挺费劲的. 题目描述: Compare two version numbers version1 and version2.If v ...
- mysql优化器在统计全表扫描的代价时的方法
innodb 的聚集索引 的叶子结点 存放的 是 索引值以及数据页的偏移量 那么在计算全表扫描的代价是怎么计算的呢? 我们知道代价 为 cpu代价+io代价 cpu代价 就是 每5条记录比对 计算一个 ...
- [LeetCode] 165. Compare Version Numbers 比较版本数
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
随机推荐
- find按照文件大小查找
例如,find -size +1M:查找大于 1 MB 的文件.其他参数: b: 512-byte blocks. This is the default if no unit is specifie ...
- selenium与appium怎样联系
appium是手机app端的自动化,它继承了webdriver(也就是selenium 2) 不过appium仍然需要通过selenium最后做测试工具,但是appium起到了一个连 ...
- JSON对象及方法
1.JSON JSON 包括 JSON 字符串和 JSON 对象.JSON 通常用于与服务端交换数据,在给服务器接收和发送数据时用的都是字符串,可以是 JSON 字符串或者一般的键值对字符串.把Jav ...
- CSP-S2019退役记/爆内存记
DAY 0 准备出发. 出发前教练说遇到事不慌,打电话,又听教练说了说历年赶车经历. 然后这趟路上居然没有什么大事. 在车上有些闲,于是就和其他人聊了会天,聊着聊着没意思,就用手机翻博客园. 这样就不 ...
- socket | netcat 模拟
#!/opt/local/bin/python2.7 #coding=utf-8 ''' 取代netcat 两台主机中其中一台控制另一台 得到北控方的shell ''' import sys impo ...
- Gym-100676F Palindrome
原题连接:https://odzkskevi.qnssl.com/1110bec98ca57b5ce6aec79b210d2849?v=1491063604 题意: 多组输入,每一次输入一个n(字符串 ...
- ceph安装过程
创建群集[2019-03-20 18:35:04,232][ceph_deploy.conf][DEBUG ] found configuration file at: /home/sceph/.ce ...
- OpenCV2.4.8 + CUDA7.5 + VS2013 配置
配置过程主要参考:https://initialneil.wordpress.com/2014/09/25/opencv-2-4-9-cuda-6-5-visual-studio-2013/ 1.为什 ...
- Yahoo! 35条网站性能优化建议
Yahoo! 35条网站性能优化建议 Yahoo!的 Exceptional Performance团队为改善 Web性能带来最佳实践.他们为此进行了一系列的实验.开发了各种工具.写了大量的文章和博客 ...
- C++ 关于const引用的测试
C++ 关于const引用的测试 今天学习了<C++ primer>第五版中的const相关内容,书中关于const的部分内容如下: 由书中内容(P55~P56)可知,const引用有如下 ...