题目描述:

自己的提交:

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. java 根据excel模板导出文件

    <!--读取excel文件,配置POI框架的依赖--> <dependency> <groupId>org.apache.poi</groupId> & ...

  2. leetcode-166周赛-5282-转化为全0矩阵的最小反转次数

    题目描述: 方法一:暴力BFS class Solution: def minFlips(self, mat) -> int: R, C = len(mat), len(mat[0]) def ...

  3. 【和孩子一起学编程】 python笔记--第五天

    关于python2在python3中的改动: https://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5MDEyMDk4Mw==&appmsgid=1 ...

  4. 关于iphone设置显示模式为标准模式和放大模式时的区别

    参考来自:https://www.jianshu.com/p/5f61d914114b CGFloat scale = [[UIScreen mainScreen] scale]; CGFloat n ...

  5. LDD3 第11章 内核的数据类型

    考虑到可移植性的问题,现代版本的Linux内核的可移植性是非常好的. 在把x86上的代码移植到新的体系架构上时,内核开发人员遇到的若干问题都和不正确的数据类型有关.坚持使用严格的数据类型,并且使用-W ...

  6. HDU 6040 Hints of sd0061 —— 2017 Multi-University Training 1

    Hints of sd0061 Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  7. HihoCoder - 1664 (单调队列)

    题目:https://vjudge.net/contest/319166#problem/B 题意: 一个01间隔矩阵,求一个方阵的最大边长,这个方阵的要求是里面01分隔,不能有01相邻 思路:同   ...

  8. 从xxxx检测到有潜在危险的 Request.Form 提示黄页

    相信很多朋友都遇到过"从客户端xxxxxx"检测到有潜在危险的 Request.Form 然后给一个黄页提示.然后检测代码又找不到错误的代码提示. 原因:是因为在页面里边使用了富文 ...

  9. Codeforces 510C (拓扑排序)

    原题:http://codeforces.com/problemset/problem/510/C C. Fox And Names time limit per test:2 seconds mem ...

  10. Codeforces Round #506 (Div. 3) E

    Codeforces Round #506 (Div. 3) E dfs+贪心 #include<bits/stdc++.h> using namespace std; typedef l ...