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 ...
随机推荐
- boost variant
Boost Variant resembles union. You can store values of different types in a boost::variant. 1. #incl ...
- WIN10无法识别安卓设备,提示Windows 无法验证此设备所需的驱动程序的数字签名
在设备管理器,显示ANDROID设备是感叹号, 不管更新驱动,还是下载什么手机助手自动安装驱动,均不可解. 从属性中查看提示的是“Windows 无法验证此设备所需的驱动程序的数字签名”, 解决办法: ...
- PHP curl_getinfo函数
curl_getinfo — 获取一个cURL连接资源句柄的信息 说明 mixed curl_getinfo ( resource $ch [, int $opt = 0 ] ) 获取最后一次传输的相 ...
- Leetcode_897. Increasing Order Search Tree
题目:https://leetcode.com/problems/increasing-order-search-tree/ 题意: 将一棵二叉搜索树,重排为一棵递增的二叉排序树. 解法1: rson ...
- 【LeetCode 85】最大矩形
题目链接 [题解] 把所有的"1"矩形分成m类. 第j类的矩形.他的右边界跟第j列紧靠. 那么. 我们设f[i][j]表示(i,j)这个点往左最长能延伸多少个数目的"1& ...
- Oil Deposits( hdu1241
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/L Oil Deposits Time Limit:1000MS ...
- xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance
运行xcode命令报错: sh-3.2# xcodebuild xcode-select: error: tool 'xcodebuild' requires Xcode, but active de ...
- dubbo远程服务调用和maven依赖的区别
dubbo:跨系统通信.比如:两个系统,一个系统A作客户端,一个系统B作服务器, 服务器B把自己的接口定义提供给客户端A,客户端A将接口定义在spring中的bean.客户端A可直接使用这个bean, ...
- asp.net的处理机制(.ashx/.aspx)
浅谈自己对asp.net 处理机制的图解 图解的内容有点多(包含asp.net 的处理机制和页面生命周期的重要事件,建议小伙伴把图片下载查看可好?) asp.net处理机制解说 当浏览器发送一条请求给 ...
- 什么是Spring Boot?
什么是Spring Boot? Spring Boot是Spring开源组织下的子项目,是Spring组件一站式解决方案,主要是简化了使用Spring的难度,简省了繁重的配置,提供了各种启动器,开发者 ...