leetcode-167周赛-1292-元素和小于等于阈值的正方形的最大边长
题目描述;
自己的提交:超时
class Solution:
def maxSideLength(self, mat: List[List[int]], threshold: int) -> int:
m,n = len(mat),len(mat[0])
res = 0
for i in range(m):
for j in range(n):
i_,j_ = i+res,j+res
while i_ < m and j_ < n:
pre = sum(sum(mat[x][j:j_])for x in range(i,i_))
cur = pre + sum(mat[x][j_] for x in range(i,i_+1)) + sum(mat[i_][y] for y in range(j,j_+1)) - mat[i_][j_]
if cur <= threshold:
res = max(res,j_-j+1)
else:
break
pre = cur
i_ += 1
j_ += 1
return res
方法一:前缀和+二分 O(mn*logmin(m,n))
class Solution:
def maxSideLength(self, mat: List[List[int]], threshold: int) -> int:
m, n = len(mat), len(mat[0])
P = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
P[i][j] = P[i - 1][j] + P[i][j - 1] - P[i - 1][j - 1] + mat[i - 1][j - 1] def getRect(x1, y1, x2, y2):
return P[x2][y2] - P[x1 - 1][y2] - P[x2][y1 - 1] + P[x1 - 1][y1 - 1] l, r, ans = 1, min(m, n), 0
while l <= r:
mid = (l + r) // 2
find = any(getRect(i, j, i + mid - 1, j + mid - 1) <= threshold for i in range(1, m - mid + 2) for j in range(1, n - mid + 2))
if find:
ans = mid
l = mid + 1
else:
r = mid - 1
return ans
方法二:O(mn)
class Solution:
def maxSideLength(self, mat: List[List[int]], threshold: int) -> int:
m, n = len(mat), len(mat[0])
P = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
P[i][j] = P[i - 1][j] + P[i][j - 1] - P[i - 1][j - 1] + mat[i - 1][j - 1] def getRect(x1, y1, x2, y2):
return P[x2][y2] - P[x1 - 1][y2] - P[x2][y1 - 1] + P[x1 - 1][y1 - 1] r, ans = min(m, n), 0
for i in range(1, m + 1):
for j in range(1, n + 1):
for c in range(ans + 1, r + 1):
if i + c - 1 <= m and j + c - 1 <= n and getRect(i, j, i + c - 1, j + c - 1) <= threshold:
ans += 1
else:
break
return ans
leetcode-167周赛-1292-元素和小于等于阈值的正方形的最大边长的更多相关文章
- LeetCode:存在重复元素【217】
LeetCode:存在重复元素[217] 题目描述 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 示例 ...
- 前端与算法 leetcode 27.移除元素
目录 # 前端与算法 leetcode 27.移除元素 题目描述 概要 提示 解析 算法 @(目录) # 前端与算法 leetcode 27.移除元素 题目描述 27.移除元素 概要 题目本身其实挺简 ...
- Leetcode:169. 多数元素
Leetcode:169. 多数元素 传送门 思路 一开始想到的一个很简单的做法就是hash法,直接利用打表记录次数再输出结果. 而利用BM算法可以令算法复杂度同样也在\(O(n)\)的情况下,将空间 ...
- leetcode——217. 存在重复元素
leetcode--217. 存在重复元素 题目描述:给定一个整数数组,判断是否存在重复元素. 如果存在一值在数组中出现至少两次,函数返回 true .如果数组中每个元素都不相同,则返回 false ...
- leetcode 双周赛9 找出所有行中最小公共元素
给你一个矩阵 mat,其中每一行的元素都已经按 递增 顺序排好了.请你帮忙找出在所有这些行中 最小的公共元素. 如果矩阵中没有这样的公共元素,就请返回 -1. 示例: 输入:mat = [[,,,,] ...
- leetcode 167 two sum II
167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascendi ...
- 【LeetCode】移除元素(Remove Element)
这道题是LeetCode里的第27道题. 题目描述: 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原 ...
- LeetCode 167:两数之和 II - 输入有序数组 Two Sum II - Input array is sorted
公众号: 爱写bug(ID:icodebugs) 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index ...
- Leetcode 219. 存在重复元素 II
说明: 首先,这是一道Easy题,我天!但是题意理解还是很多坑~ 题目描述: 给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j] ...
随机推荐
- MySQL的性能指标计算和优化方法
MySQL的性能指标计算和优化方法1 QPS计算(每秒查询数) 针对MyISAM引擎为主的DB mysql> show global status like 'questions';+----- ...
- (转)用C#实现实现简单的 Ping 的功能,用于测试网络是否已经连通
本文转载自:http://blog.csdn.net/xiamin/archive/2009/02/14/3889696.aspx 用C#实现实现简单的 Ping 的功能,用于测试网络是否已经联通 1 ...
- 解决Chrome网页编码显示乱码的问题
解决Chrome网页编码显示乱码的问题 记得在没多久以前,Google Chrome上面出现编码显示问题时,可以手动来调整网页编码问题,可是好像在Chrome 55.0版以后就不再提供手动调整编码,所 ...
- Learn Python the hard way, ex45 对象、类、以及从属关系
#!/usr/bin/python #coding:utf-8 # animal is-a object(yes,sort of sonfusing)look at the extra credit ...
- 19c的 rac在oracle linux7.4上搭建总结
准备: 1,ASM磁盘空间最低要求OCR的磁盘占用需求有了明显增长.为了方便操作,设置如下:External: 1个卷x40GNormal: 3个卷x30GHight: 5个卷x25GFlex: 3个 ...
- 001/Docker入门(Mooc)
docker官网:https://www.docker.com/ 1.什么是docker 2.Docker思想 ==> [1].集装箱:保证程序完整(不缺东西,如配置文件等). [2]. ...
- Nginx配置之rewrite、proxy_pass、upstream、location
如图,这是Nginx的配置文件nginx.conf中的一段配置代码. 在http段中定义了一个名为webservers的upstream模块,主要用于负载均衡. 在server模块中,定义了一个loc ...
- Struts2异常:HTTP Status 404 - /Struts2/book/addBook.action
HTTP Status 404 - /Struts2/book/addBook.action 如果在Struts2的框架中访问路径出现了这个错误,可能存在的原因有如下的两个: 1. 路径写错,也就是a ...
- 数据映射-LSM Tree和SSTable
Coming from http://blog.sina.com.cn/s/blog_693f08470101njc7.html 今天来聊聊lsm tree,它的全称是log structured m ...
- 加密算法:DES、AES等
指标:运算速度.安全性.资源消耗 对称加密算法(加解密密钥相同): 非对称算法(加密密钥和解密密钥不同): 散列算法比较: 对称与非对称算法比较: 算法选择(从性能和安全性综合) 对称加密: AES( ...