题目描述:

自己的提交:

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的正方形子矩阵的更多相关文章

  1. PHP算法之统计全为 1 的正方形子矩阵

    在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0 输出: 4 来源:力扣( ...

  2. 51nod 1158 全是1的最大子矩阵

    题目链接:51nod 1158 全是1的最大子矩阵 题目分类是单调栈,我这里直接用与解最大子矩阵类似的办法水过了... #include<cstdio> #include<cstri ...

  3. 51nod1158 全是1的最大子矩阵

    跟最大子矩阵差不多O(n3)扫一下.有更优写法?挖坑! #include<cstdio> #include<cstring> #include<cctype> #i ...

  4. 51Nod 1158 全是1的最大子矩阵 —— 预处理 + 暴力枚举 or 单调栈

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1158 1158 全是1的最大子矩阵  基准时间限制:1 秒 空 ...

  5. [LeetCode] Count Binary Substrings 统计二进制子字符串

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  6. [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数

    题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...

  7. leetcode 165

    才一周没刷leetcode,手就生了,这个题目不难,但是完全AC还是挺费劲的. 题目描述: Compare two version numbers version1 and version2.If v ...

  8. mysql优化器在统计全表扫描的代价时的方法

    innodb 的聚集索引 的叶子结点 存放的 是 索引值以及数据页的偏移量 那么在计算全表扫描的代价是怎么计算的呢? 我们知道代价 为 cpu代价+io代价 cpu代价 就是 每5条记录比对 计算一个 ...

  9. [LeetCode] 165. Compare Version Numbers 比较版本数

    Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...

随机推荐

  1. find按照文件大小查找

    例如,find -size +1M:查找大于 1 MB 的文件.其他参数: b: 512-byte blocks. This is the default if no unit is specifie ...

  2. selenium与appium怎样联系

    appium是手机app端的自动化,它继承了webdriver(也就是selenium 2)          不过appium仍然需要通过selenium最后做测试工具,但是appium起到了一个连 ...

  3. JSON对象及方法

    1.JSON JSON 包括 JSON 字符串和 JSON 对象.JSON 通常用于与服务端交换数据,在给服务器接收和发送数据时用的都是字符串,可以是 JSON 字符串或者一般的键值对字符串.把Jav ...

  4. CSP-S2019退役记/爆内存记

    DAY 0 准备出发. 出发前教练说遇到事不慌,打电话,又听教练说了说历年赶车经历. 然后这趟路上居然没有什么大事. 在车上有些闲,于是就和其他人聊了会天,聊着聊着没意思,就用手机翻博客园. 这样就不 ...

  5. socket | netcat 模拟

    #!/opt/local/bin/python2.7 #coding=utf-8 ''' 取代netcat 两台主机中其中一台控制另一台 得到北控方的shell ''' import sys impo ...

  6. Gym-100676F Palindrome

    原题连接:https://odzkskevi.qnssl.com/1110bec98ca57b5ce6aec79b210d2849?v=1491063604 题意: 多组输入,每一次输入一个n(字符串 ...

  7. ceph安装过程

    创建群集[2019-03-20 18:35:04,232][ceph_deploy.conf][DEBUG ] found configuration file at: /home/sceph/.ce ...

  8. OpenCV2.4.8 + CUDA7.5 + VS2013 配置

    配置过程主要参考:https://initialneil.wordpress.com/2014/09/25/opencv-2-4-9-cuda-6-5-visual-studio-2013/ 1.为什 ...

  9. Yahoo! 35条网站性能优化建议

    Yahoo! 35条网站性能优化建议 Yahoo!的 Exceptional Performance团队为改善 Web性能带来最佳实践.他们为此进行了一系列的实验.开发了各种工具.写了大量的文章和博客 ...

  10. C++ 关于const引用的测试

    C++ 关于const引用的测试 今天学习了<C++ primer>第五版中的const相关内容,书中关于const的部分内容如下: 由书中内容(P55~P56)可知,const引用有如下 ...