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 <= 300m == mat.lengthn == mat[i].length0 <= mat[i][j] <= 100000 <= 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 ...
随机推荐
- dataframe中的数据类型及转化
1 float与str的互化 import pandas as pd import numpy as np df = pd.DataFrame({'a':[1.22, 4.33], 'b':[3.44 ...
- 小白学数据分析--聚类分析理论之K-means理论篇
小白学数据分析--聚类分析理论之K-means理论篇 聚类分析是一类广泛被应用的分析方法,其算法众多,目前像SAS.Splus.SPSS.SPSS Modeler等分析工具均以支持聚类分析,但是如何使 ...
- Charls
1.连接设备 charles--proxy--proxy setting 设置端口号 charles--proxy--ssl proxy setting 设置代理域名 [pc端] charles--p ...
- SpringBoot中FreeMarker创建
1.创建SpringBoot项目时,勾选freemarker依赖(web,dev) 2.Controller中向model中存放数据 package com.ziv.springbootbygrald ...
- Mybatis-第N篇配置log4j1、log4j2打印执行的sql语句
1.log4j1配置 目录结构: conf.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCT ...
- python 更快地判断数字的奇数还是偶数
使用 按位与运算符(&) 将能更加快速地判断一个整数是奇数还是偶数 使用举例如下: def check_number(n): if n & 1: return '奇数' else: r ...
- H-Updating a Dictionary (模拟)
In this problem, a dictionary is collection of key-value pairs, where keys are lower-case letters, a ...
- 电路维修 (广搜变形-双端队列bfs)
# 2632. 「BalticOI 2011 Day1」打开灯泡 Switch the Lamp On [题目描述] 有一种正方形的电路元件,在它的两组相对顶点中,有一组会用导线连接起来,另一组则不会 ...
- Angular ngTemplateOutlet
虽然我们可以通过使用 ViewContainerRef 将 ElementRef创建的视图插入指定的位置,但是仍然希望有某中快捷的方式帮我们实现. ngTemplateOutlet与ngCompone ...
- Java编码技巧与代码优化
本文参考整理自https://mp.weixin.qq.com/s/-u6ytFRp-ZAqdLBsMmuDMw 对于在本文中有所疑问的点可以去该文章查看详情 常量&变量 直接赋值常量值, 禁 ...