题目描述:

自己的提交:

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. head、tail 命令和实时(real-time)更新

    head.tail 命令和实时(real-time)更新 head filename: 输出 filename 文件的前10项 tail filename: 输出 filename 文件的后10项 即 ...

  2. Erlang/Elixir精选-第4期(20191223)

    精选文章 A digital symphony - The architecture of API Fortress. 使用Actor模型来支持基于微服务的大规模分布式软件架构.用实例解释了Actor ...

  3. gensim word2vec |来自渣渣硕的学习笔记

    最近写论文跑模型,要用到word2vec,但是发现自己怎么也看不懂网上的帖子,还是自己笨吧,所以就有了我的第一篇博客!!!  关于word2vec工具打算写一个系列的,当然今天这篇文章只打算写: 如何 ...

  4. Python3解leetcode Factorial Trailing Zeroes

    问题描述: Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 ...

  5. webservice文件上传下载(byte[] 实现方式)

    测试环境:axis2-1.6.1.6.0.20.jdk1.5 说明:本方式仅适用于文件小于10M的场景(否则会出现内存溢出),大文件的上传下载应另选其他方式. 1.创建要发布成webservice的j ...

  6. sublime常用基础插件合集

    插件介绍 Package Control 功能:安装包管理简介:sublime插件控制台,提供添加.删除.禁用.查找插件等功能使用方法:快捷键 Ctrl+Shift+P,输入 install 选中In ...

  7. error C2664: “ATL::CStringT<BaseType,StringTraits>::Remove”: 不能将参数 1 从“const char [2]”转换为“char”

    转自VC错误:http://www.vcerror.com/?p=1395 问题描述: 代码: CString str("asdfafda"); str.Remove(" ...

  8. 科普:std::sort干了什么

    std::sort算是STL中对OIer比较友好的函数了,但你有想过sort是如何保证它的高速且稳定吗? 正文 我们首先来到第一层:sort函数 template<typename _Rando ...

  9. 利用Graphziv帮助理解复杂的类层次关系

    最近在学习osg三维视景仿真平台,学习的过程中涉及到许多的类与类之间的继承和包含关系.在复杂点的例子中,许多的类和节点组合在一起,很容易让人迷失方向.在编译源代码的时候,无意间发现了Graphviz这 ...

  10. Vue.use

    不管是对象还是函数install 是Vue.use()必须要有的方法 否则无法使用(Vue.use(MintUI))但axios 不需要Vue.use(axios) 可以直接使用 因为axios没有i ...