题目链接

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

  


给定一个矩阵,找出一个最大正方形的边长,这个正方形中元素和要小于threshold。

解法:

二维前缀和,sum[i][j]表示从[0][0]到[i][j]的矩形的元素和,有了这个sum后,就可以用 O(row * col * min(row,col)) 的时间复杂度遍历所有的正方形。

class Solution {
public:
int maxSideLength(vector<vector<int>>& mat, int threshold) {
int row = mat.size(), col = mat[].size();
vector<vector<int>> sum(row+, vector<int>(col+, ));
for(int i=; i<=row; i++)
for(int j=; j<=col; j++)
sum[i][j] = sum[i-][j]+sum[i][j-]-sum[i-][j-]+mat[i-][j-];
int ret = ;
for(int i=; i<=row; i++)
for(int j=; j<=col; j++){
for(int k=; k<=min(i, j); k++){
int temp = sum[i][j]-sum[i-k][j]-sum[i][j-k]+sum[i-k][j-k];
if(temp <= threshold)
ret = max(ret, k);
}
}
return ret;
}
};

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

    题目如下: Given a m x n matrix mat and an integer threshold. Return the maximum side-length of a square ...

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

  3. [Bug]The maximum array length quota (16384) has been exceeded while reading XML data.

    写在前面 在项目中,有客户反应无法正常加载组织结构树,弄了一个测试的程序,在日志中查看到如下信息: Error in deserializing body of reply message for o ...

  4. .NET上传大文件时提示Maximum request length exceeded错误的解决方法

    使用IIS托管应用程序时,当我们需要上传大文件(4MB以上)时,应用程序会提示Maximum request length exceeded的错误信息.该错误信息的翻译:超过最大请求长度. 解决方法: ...

  5. 【应用服务 App Service】App Service中上传文件/图片(> 2M)后就出现500错误(Maximum request length exceeded).

    问题描述 在使用App Service (Windows)做文件/图片上传时候,时常遇见上传大文件时候出现错误,这是因为IIS对文件的大小由默认限制.当遇见(Maximum request lengt ...

  6. 【LightOJ 1081】Square Queries(二维RMQ降维)

    Little Tommy is playing a game. The game is played on a 2D N x N grid. There is an integer in each c ...

  7. IIS: 配置web.config解决Maximum request length exceeded错误

    In system.web <httpRuntime maxRequestLength="1048576" executionTimeout="3600" ...

  8. LeetCode: 221_Maximal Square | 二维0-1矩阵中计算包含1的最大正方形的面积 | Medium

    题目: Given a 2D binary matrix filled with 's and return its area. For example, given the following ma ...

  9. qrcode length overflow 生成二维码网址长度溢出解决办法

    QRCode.js is javascript library for making QRCode. QRCode.js supports Cross-browser with HTML5 Canva ...

随机推荐

  1. 《图解设计模式》读书笔记7-2 Mediator模式

    目录 Mediator模式简介 示例程序 示例程序类图 代码 Mediator模式角色和类图 角色 模式类图 思路拓展 简单化 角色复用 Mediator模式简介 Mediator模式即中介者模式,可 ...

  2. UI自动化之异常与截图处理

    对操作不成功时,希望能够继续执行其他操作,或者是,希望操作不成功时,能够写日志记录 目录 1.常见异常 2.截图处理 1.常见异常 1.NoSuchElementException:没有找到元素 2. ...

  3. 【转载】如何编写ROS的第一个程序hello_world

    目录 1.工作空间的创建 2.功能包的创建 3.功能包的源代码编写 4.功能包的编译配置 5.功能包的编译 6.功能包的启动运行 既然ROS已经成功安装好了,大家一定很想亲自动动手编一个通过起手式例程 ...

  4. 五分钟搞懂 Linux 重点知识,傻瓜都能学会!

    来源:无痴迷,不成功 www.cnblogs.com/justmine/p/9053419.html 写在前面 我们都知道Linux是一个支持多用户.多任务的系统,这也是它最优秀的特性,即可能同时有很 ...

  5. [Luogu 4688] [Ynoi2016]掉进兔子洞 (莫队+bitset)

    [Luogu 4688] [Ynoi2016]掉进兔子洞 (莫队+bitset) 题面 一个长为 n 的序列 a.有 m 个询问,每次询问三个区间,把三个区间中同时出现的数一个一个删掉,问最后三个区间 ...

  6. weight (搜索对象的选取)

    #10249. 「一本通 1.3 例 5」weight [题目描述] 已知原数列 $a_1,a_2,\cdots,a_n$​​ 中的前 1项,前 2 项,前3项, $\cdots$ ,前 n 项的和, ...

  7. 机器学习ROC图解读

    1. 分类器评估指标 对于二分类问题,可将样例根据其真实类别和分类器预测类别划分为:真正例(True Positive,TP):真实类别为正例,预测类别为正例.假正例(False Positive,F ...

  8. Linux下创建虚VIP的方法及相互的区别:

    #创建虚VIPifconfig eth1:1 192.168.202.200 broadcast 192.168.202.255 netmask 255.255.255.0 up ip addr ad ...

  9. BUUCTF--rsa

    测试文件:https://buuoj.cn/files/ed10ec009d5aab0050022aee131a7293/41c4e672-98c5-43e5-adf4-49d75db307e4.zi ...

  10. spark复习笔记(4):RDD变换

    一.RDD变换 1.返回执行新的rdd的指针,在rdd之间创建依赖关系.每个rdd都有一个计算函数和指向父rdd的指针 Spark是惰性的,因此除非调用某个转换或动作,否则不会执行任何操作,否则将触发 ...