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 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_[二维前缀和]的更多相关文章
- 【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 ...
- 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< ...
- [Bug]The maximum array length quota (16384) has been exceeded while reading XML data.
写在前面 在项目中,有客户反应无法正常加载组织结构树,弄了一个测试的程序,在日志中查看到如下信息: Error in deserializing body of reply message for o ...
- .NET上传大文件时提示Maximum request length exceeded错误的解决方法
使用IIS托管应用程序时,当我们需要上传大文件(4MB以上)时,应用程序会提示Maximum request length exceeded的错误信息.该错误信息的翻译:超过最大请求长度. 解决方法: ...
- 【应用服务 App Service】App Service中上传文件/图片(> 2M)后就出现500错误(Maximum request length exceeded).
问题描述 在使用App Service (Windows)做文件/图片上传时候,时常遇见上传大文件时候出现错误,这是因为IIS对文件的大小由默认限制.当遇见(Maximum request lengt ...
- 【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 ...
- IIS: 配置web.config解决Maximum request length exceeded错误
In system.web <httpRuntime maxRequestLength="1048576" executionTimeout="3600" ...
- 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 ...
- qrcode length overflow 生成二维码网址长度溢出解决办法
QRCode.js is javascript library for making QRCode. QRCode.js supports Cross-browser with HTML5 Canva ...
随机推荐
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第4节 ArrayList集合_17-ArrayList练习二_存储自定义
分析题目和解题思路 先来新建学生类.定义两个成员变量,后面进行代码的生成 遍历集合
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_10_字节输入流一次读取一个字节的原理
原理解析 创建一个字节流,指向读取文件的第一个字节. read找jvm,jvm找os.os去读取硬盘.,读取后指正向后移动一位
- 测开之路一百零九:bootstrap列表
bootstrap列表 引入bootstrap标签 原本的效果 水平显示 bootstrap列表 列表组合框 在组合框后面加备注 突出显示 a标签列表 <!DOCTYPE html>< ...
- 【MM系列】SAP MM模块-移动类型全部列表
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-移动类型全部列表 ...
- python基础数据类型补充以及编码的进阶
一.基本数据类型的补充循环列表改变列表大小的问题#请把列表中索引为基数的元素写出l1=[1,2,3,4,5,6]for i in l1: if i%2!=0: print(i)结果:135二:基本数据 ...
- pyAudioAnalysis-audioFeatureExtraction 错误纠正
1. TypeError: mfccInitFilterBanks() takes 2 positional arguments but 7 were given The issue In the f ...
- Vue的生命周期(在其他地方看到一份非常好又详细的详解)
链接地址:https://segmentfault.com/a/1190000011381906 首先,每个Vue实例在被创建之前都要经过一系列的初始化过程,这个过程就是vue的生命周期.首先看一张图 ...
- python基础-6 正则表达式
一 python正则简介 就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现. 正则表达式模式被编译成一系列的字 ...
- STL hash function的模板特化
哈希函数的作用是将一个值映射为一个哈希值,从而根据这个哈希值,在哈希表中对数据进行定位. template <class _Val, class _Key, class _HashFcn, cl ...
- MySQL-第十四篇事务管理
1.什么是事务 事务是由一步或者几步数据库操作序列组成的逻辑执行单元,这系列操作要么全部执行,要么全部放弃执行. 2.事务具备的4个特性: 1>原子性(Atomicity):事务是应用中最小的执 ...