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] ...
随机推荐
- JDBC之Statement 接口的测试(存在sql注入风险)
实现简单的登录功能 import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; impo ...
- daemon(守护、服务员)-t1.setDaemon(true) - 设置为守护线程
daemon(守护.服务员)t1.setDaemon(true) - 设置为守护线程 class KTV extends Thread{ public void run(){ try { Thread ...
- 包管理器 - peer dependency 的安装
npm 和 yarn 安装依赖(包)时不会自动安装 peer dependence(虽然很旧的 npm 是会自动安装的,但几乎没人用那么旧的了),而是给出如下警告: $ npm install --s ...
- nodejs 格式化 Date() 为yyyy-MM-dd HH:mm:ss 格式
===============2019-11-25更新======== 推荐:更实用完美解决时间格式化的 组件 monent 官网地址:http://momentjs.cn/ ============ ...
- xml、Json生成cs代码文件
一:xml生成cs实体类 1.开始菜单>Visual Studio 2015> Visual Studio Tools>VS2015 开发人员命令提示 2.xsd xmlFileNa ...
- vue组件通信之父子组件通信
准备工作: 首先,新建一个项目,如果这里有不会的同学,可以参考我转载过的文章 http://www.cnblogs.com/Sky-Ice/p/8875958.html vue 脚手架安装及新建项目 ...
- [Markdown] 02 简单应用 第二弹
目录 4. 插入链接 4.1 Markdown 的方式 用法 1 用法 2 4.2 HTML5 的方法 用法 1 用法 2 5. 插入图片 5.1 使用网络地址 5.2 使用本地地址 5.2.1 小括 ...
- 【Python—字典的用法】创建字典的3种方法
#创建一个空字典 empty_dict = dict() print(empty_dict) #用**kwargs可变参数传入关键字创建字典 a = dict(one=1,two=2,three=3) ...
- python的cls,self,classmethod,staticmethod
python类里会出现这三个单词,self和cls都可以用别的单词代替,类的方法有三种, 一是通过def定义的 普通的一般的,需要至少传递一个参数,一般用self,这样的方法必须通过一个类的实例去访问 ...
- C Yuhao and a Parenthesis
Yuhao and a Parenthesis time limit per test 2 seconds memory limit per test 256 megabytes input stan ...