1 Star 0 Fork 0

ZENGWatermelon/LeetCode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
1292.元素和小于等于阈值的正方形的最大边长.py 1.77 KB
一键复制 编辑 原始数据 按行查看 历史
sunzhaoc 提交于 2020-11-24 11:23 . master
'''
Description:
Version: 1.0
Autuor: Vicro
Date: 2020-10-08 21:18:56
LastEditTime: 2020-10-13 21:55:31
FilePath: /Leetcode/Chinese/1292.元素和小于等于阈值的正方形的最大边长.py
'''
#
# @lc app=leetcode.cn id=1292 lang=python3
#
# [1292] 元素和小于等于阈值的正方形的最大边长
#
# @lc code=start
class Solution:
def maxSideLength(self, mat, threshold):
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 = 1, min(m, n)
ans = 0
find = False
while l <= r:
mid = (r - l) // 2 + l
# for i in range(0, m - mid + 1):
# for j in range(0, n - mid + 1):
# A = getRect(i + 1, j + 1, i + mid, j + mid)
# if A <= threshold:
# find = True
# break
# if A <= threshold:
# break
# find = any(getRect(i + 1, j + 1, i + mid, j + mid) <= threshold for i in range(0, m - mid + 1) for j in range(0, m - mid + 1))
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
# find = False
else:
r = mid - 1
return ans
sol = Solution()
A = sol.maxSideLength([[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], 4)
print(A)
# @lc code=end
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/watermelonTT/LeetCode.git
git@gitee.com:watermelonTT/LeetCode.git
watermelonTT
LeetCode
LeetCode
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385