题目如下:

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. 利用Python进行数据分析 第4章 IPython的安装与使用简述

    本篇开始,结合前面所学的Python基础,开始进行实战学习.学习书目为<利用Python进行数据分析>韦斯-麦金尼 著. 之前跳过本书的前述基础部分(因为跟之前所学的<Python基 ...

  2. Python中datetime库的用法

    datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和MINYEAR,分别是9999和1. datetime模块定义了5个类,分别是 1.datetime ...

  3. MySQL AND 和 OR 联合使用带来的坑

    MySQL 基础篇 三范式 MySQL 军规 MySQL 配置 MySQL 用户管理和权限设置 MySQL 常用函数介绍 MySQL 字段类型介绍 MySQL 多列排序 MySQL 行转列 列转行 M ...

  4. hdu 5230 整数划分 dp

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5230 题意:给定n,c,l,r.求有多少种方法从1~n-1选取任意k数每个数的权重为其下标,使得这些数字之 ...

  5. Java数据结构HashMap

    java数据结构HashMap /** * <html> * <body> * <P> Copyright JasonInternational</p> ...

  6. C#工厂模式案例

    class JianDanGongChang { static void Main(string[] args) { Factory factory=new LianXiangFactory(); D ...

  7. Linux每隔1秒kill掉cpu大于50%的进程

    1.新建/test/killcpu.sh shell脚本 并授予权限0755#!/bin/bashps axf -o "pid %cpu" | awk '{if($2>=50 ...

  8. ThinkPad T420i 上 Ubuntu 12.04 实现指纹识别登录

    ThinkPad T420i 上 Ubuntu 12.04 实现指纹识别登录 # add ppa add-apt-repository ppa:fingerprint/fprint # update ...

  9. ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 Areas区域说明

    https://www.cnblogs.com/webapi/p/5976642.html Asp.Net MVC Areas区域说明   一般网站分为前台+会员后台+管理员后台,做过webform的 ...

  10. IOC实现-Unity

    .NET中实现IOC有很多方式,比如:Unity.Ninject.Autofac.MEFNinject的实现参考<Pro ASP.NET MVC3.5 FrameWork>下面给出的是Un ...