【leetcode】1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold
题目如下:
Given a
m x n
matrixmat
and an integerthreshold
. Return the maximum side-length of a square with a sum less than or equal tothreshold
or 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: 0
Example 3:
- Input: mat = [[1,1,1,1],[1,0,0,0],[1,0,0,0],[1,0,0,0]], threshold = 6
- Output: 3
Example 4:
- Input: mat = [[18,70],[61,1],[25,85],[14,40],[11,96],[97,96],[63,45]], threshold = 40184
- Output: 2
Constraints:
1 <= m, n <= 300
m == mat.length
n == mat[i].length
0 <= mat[i][j] <= 10000
0 <= 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 ...
随机推荐
- 安全篇-AES/RSA加密机制
在服务器与终端设备进行HTTP通讯时,常常会被网络抓包.反编译(Android APK反编译工具)等技术得到HTTP通讯接口地址和参数.为了确保信息的安全,我们采用AES+RSA组合的方式进行接口参数 ...
- 通过pip命令导出和导入Python环境安装包
我们在开发完代码后,一般需要将依赖包导出,然后在移植到其他系统使去安装,保证环境正常 导出Python环境安装包[root@bogon ~]# pip freeze > packages.t ...
- jq获取元素偏移量offset()
解释: 1 获取匹配元素在当前视口的相对偏移. 2 返回的对象包含两个整型属性:top 和 left demo1: 获取top与left var aaa = $(".aaa "); ...
- (一)Activiti简介
一.概念 Activiti项目是一项新的基于Apache许可的开源BPM平台,从基础开始构建,旨在提供支持新的BPMN 2.0标准,包括支持对象管理组(OMG),面对新技术的机遇,诸如互操作性和云架构 ...
- IdentityServer4密码模式接入现有用户数据表
具体接入identityserver请看文档,这里只简单列举部分步骤 1.创建一个web项目,引入Identityserver4的nuget包 2.新建一个类,实现IResourceOwnerPass ...
- Visual Studio 开发大量 JavaScript 代码项目程序崩溃的解决方案
最近公司做新项目,基于 Bootstrap.AngularJS 和 kendo 开发一套后台的管理系统,在项目中使用了大量的 JavaScript 文件,这两天 Visual Studio 2015 ...
- .netcore 上传
BS 上传文件,就是 <input type="file" name="file" /> 这个选择文件之后,浏览器保存了文件路径,上传的时候,把这 ...
- Angular 惰性路由
根路由上的一个 loadChildren 属性,设置为一个字符串.这样就是惰性路由了. angular6 这样写:loadChildren: './background-check/backgroun ...
- vue中子组件的created、mounted钩子中获取不到props中的值问题
父子组件通信 这个官网很清楚,也很简单,父组件中使用v-bind绑定传送,子组件使用props接收即可 例如: 父组件中: <template> <div> <head- ...
- Android Service的有关总结
来自一位网友的评论 1.使用方式 startService 启动的服务 主要用于启动一个服务执行后台任务,不进行通信.停止服务使用stopService bindService 启动的服务 该方法启动 ...