题目如下:

Given a m x n matrix mat and an integer threshold. Return the maximum side-length of a square with a sum less than or equal to threshold 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的更多相关文章

  1. 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< ...

  2. 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 ...

  3. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  4. 【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 ...

  5. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  6. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  7. 【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 ...

  8. 【Leetcode】164. Maximum Gap 【基数排序】

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  9. 【LeetCode】646. Maximum Length of Pair Chain 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...

随机推荐

  1. 数位dp踩坑

    前言 数位DP是什么?以前总觉得这个概念很高大上,最近闲的没事,学了一下发现确实挺神奇的. 从一道简单题说起 hdu 2089 "不要62" 一个数字,如果包含'4'或者'62', ...

  2. C++基础--函数重载

    函数重载定义: 在相同的作用域中具有相同的函数名而函数形参列表(参数类型.参数个数.参数顺序)不同的两个函数,称之为函数重载.注意:函数返回值类型并不是重载的条件. 函数重载优点: 可以使用相同的函数 ...

  3. centos7.6 yum安装mysql5.7版本

    由于mysql5.5及之前的版本一些项目上线报错 卸载: 首先删除centos上原来的mysql老版本,注意备份,清理干净. 之前怎么安装的清理,防止卸载不干净会有冲突. 我之前yum安装mysql5 ...

  4. PAT(B) 1017 A除以B(Java)

    题目链接:1017 A除以B 分析 读取输入的A和B后,保存为字符串.模拟除法运算过程. 不要用BigInteger,因为会超时. 另外字符串经常要扩展(例如:append())的话,不要用Strin ...

  5. python中检测某个变量是否有定义

    目录 第一种方法使用内置函数locals() 第二种方法使用内置函数dir() 第三种方法使用内置函数vars() 第一种方法使用内置函数locals() 'testvar' in locals(). ...

  6. Apache Tomcat 安装与配置教程

    JDK的安装与配置 1. 从官网下载JDK https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-213315 ...

  7. CentOS 7.X 静默安装Oracle 12C数据库

    环境 System : CentOS 7.x jrxxfwb-zrgldb://> uname -a Linux jrxxfwb-zrgldb 3.10.0-693.17.1.el7.x86_6 ...

  8. Tomcat与WAS应用中间件差异化分析研究

    --转载 http://blog.chinaunix.net/uid-25723371-id-5759072.html 目前我们在使用的基于JAVA的提供逻辑展现应用中间件有两种,一种是以商用软件WA ...

  9. Nginx 不支持WebSocket TCP

    proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade";

  10. (六)Hibernate的增删改查操作(3)

    一.在Hibernate中使用原生SQL语句 sql语句面向的是数据库,所以sql语句中对应的不再是bean了,比如sql="select * from user"   在hql中 ...