【leetcode】1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold
题目如下:
Given a
m x nmatrixmatand an integerthreshold. Return the maximum side-length of a square with a sum less than or equal tothresholdor return 0 if there is no such square.Example 1:
Input: mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], threshold = 4
Output: 2
Explanation: The maximum side length of square with sum less than 4 is 2 as shown.Example 2:
Input: mat = [[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2]], threshold = 1
Output: 0Example 3:
Input: mat = [[1,1,1,1],[1,0,0,0],[1,0,0,0],[1,0,0,0]], threshold = 6
Output: 3Example 4:
Input: mat = [[18,70],[61,1],[25,85],[14,40],[11,96],[97,96],[63,45]], threshold = 40184
Output: 2Constraints:
1 <= m, n <= 300m == mat.lengthn == mat[i].length0 <= mat[i][j] <= 100000 <= threshold <= 10^5
解题思路:记grid[i][j]为左上顶点坐标是(0,0),右下顶点坐标为(i,j) 组成的矩形中每个坐标的和,grid很容易通过遍历一次mat求得。接下来就只要求mat中每个子矩形的和,如下图所示,要求绿色部分的矩形和,假设绿色矩形的右下顶点坐标是(i,j)只需要用grid[i][j] 减去两个黄色部分的矩形和,因为红色部分是两个黄色部分共有,因此做减法的时候多减了一次,需要再加回去,最终计算公式有:grid[i][j] - 两个黄色子矩形和 + 红色子矩形和。

代码如下:
class Solution(object):
def maxSideLength(self, mat, threshold):
"""
:type mat: List[List[int]]
:type threshold: int
:rtype: int
"""
grid = [[0] * len(mat[0]) for _ in mat]
for i in range(len(mat)):
amount = 0
for j in range(len(mat[i])):
amount += mat[i][j]
grid[i][j] = amount
if i > 0:
grid[i][j] += grid[i-1][j]
#print grid res = 0
for i in range(len(grid)):
for j in range(len(grid[i])): low,high = res,min(len(grid),len(grid[0]))
while low <= high:
mid = (low + high)/2
if i + mid >= len(grid) or j + mid >= len(grid[0]):
high = mid - 1
continue
val = grid[i + mid][j + mid]
if j - 1 >= 0:
val -= grid[i + mid][j - 1]
if i - 1 >= 0:
val -= grid[i - 1][j + mid]
if i - 1 >= 0 and j - 1 >= 0:
val += grid[i - 1][j - 1]
if val <= threshold:
res = max(res, mid + 1)
low = mid + 1
else:
high = mid - 1 return res
【leetcode】1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold的更多相关文章
- LeetCode 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold
题目 我是按照边进行二分的 class Solution { public: int sum[100005]; int a[305][305]; int maxSideLength(vector< ...
- leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和]
题目链接 Given a m x n matrix mat and an integer threshold. Return the maximum side-length of a square w ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【leetcode】1239. Maximum Length of a Concatenated String with Unique Characters
题目如下: Given an array of strings arr. String s is a concatenation of a sub-sequence of arr which have ...
- 【Leetcode】164. Maximum Gap 【基数排序】
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 【LeetCode】646. Maximum Length of Pair Chain 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...
随机推荐
- c++语法笔记(中)
继承与派生 继承面向对象程序设计4个主要特征:抽象,封装,继承,多态性c++通过继承实现可重用性.有时两个类的内容基本相同或有一部分相同,这时可以利用原来声明的类作为基础,再加上新的内容即可,减少了工 ...
- LC 21. Merge Two Sorted Lists
题目描述 Merge two sorted linked lists and return it as a new list. The new list should be made by splic ...
- PHP和Memcached - Memcached的介绍及常用命令
1.什么是Memcached 自由开源的,高性能,分布式内存对象缓存系统,分布式是基于客户的缓存系统,服务器之间是不相互通讯的. 2.Memcached的使用场景 储存session. 缓存数据. 解 ...
- python笔记005-字符串-列表-元组
目录 1 上次作业补充拓展... 1 1.1 进制转换... 1 1.2 类型判断... 1 2 今日学习内容... 2 2.1 格式化输出... 2 2.2 基本运算符... 2 2.2.1 算术运 ...
- Unity场景间数据传递方法
在游戏开发中,会常用到场景间传递数据的方法(比如关卡选择,过关后自动回到关卡选择界面,以动画方式解锁下一关),目前研究了三种: 1. 使用DontDestroyOnLoad方法: (1)在场景A中做个 ...
- IDEA如何本机调试springboot应用打的jar包
背景: 我用命名行 执行 java -jar ***.jar发现 springboot启动时抛出错误,因此想debug进去看看究竟为什么出错. 1 在命令行执行 java -jar -Xdebug ...
- Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor'
An unhandled exception occurred while processing the request. InvalidOperationException: Unable to r ...
- Go part 5 结构体,方法与接收器
结构体 结构体定义 结构体的定义只是一种内存布局的描述(相当于是一个模板),只有当结构体实例化时,才会真正分配内存空间 结构体是一种复合的基本类型,通过关键字 type 定义为 自定义 类型后,使结构 ...
- [书籍翻译] 《JavaScript并发编程》第三章 使用Promises实现同步
本文是我翻译<JavaScript Concurrency>书籍的第三章 使用Promises实现同步,该书主要以Promises.Generator.Web workers等技术来讲解J ...
- vue去哪儿网项目环境配置
一.首先安装node.js 根据自己的(windows或mac)系统进行安装node,在开发环境中一般安装LTS版本.安装成功后,在终端输入"node -v"和"npm ...
