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 ...
随机推荐
- set()运算
1 计算两个list的关系时,可转化为set进行运算. 参考:https://www.runoob.com/python3/python3-set.html a =[1,4,3,5,6,6,7,7,7 ...
- js-禁止长页面滚动
标题的需求问题其实我经常遇到.尤其是在碰到页面同时出现有视频及弹层的情况. 当然我说的问题皆是针对微信H5开发的哈 IOS中,视频播放,弹层出现时,视频在弹层的下面,不会出现问题: 安卓手机中,完了, ...
- 优化 | Redis AOF重写导致的内存问题 不错
一.问题说明 业务上接到报警提示服务器内存爆了,登录查看发现机器剩余内存还很多,怀疑是被OOM了,查看/var/log/messages: kernel: [25918282.632003] Out ...
- linux eclipse 下出现undefined reference ***,在使用boost库时出现的问题
直接在eclipse下添加boost_system就可以了,这个文件有可能在库中找不到,或者名字不一样,直接使用这个名字就可以了,在setting 下
- jmeter _Random函数生成随机数
因对发送邮件接口做压测发现相同数据对服务器的压力很小所以需要每次发送请求都需要不同的参数,所以要对某个字段做随机数 选项中-函数助手对话框
- linux中编写查看内存使用率的shell脚本,并以高亮颜色输出结果
编辑脚本内容: #!/bin/bash MEMUSER=`free -m|grep -i mem|awk '{print $3/$2*100"%"}'` echo -e " ...
- [Markdown] 04 进阶语法 第二弹
[TOC] 接上一篇 [Mardkown] 03 进阶语法 第一弹 8. LaTeX 8.1 相关介绍 TeX:学术排版 LaTeX:相当于 TeX 的简化版本:对公式编辑精细至像素级别 MathJa ...
- AtCoder Beginner Contest 133 -D — Rain Flows into Dams
(https://atcoder.jp/contests/abc133/tasks/abc133_d) 思路:每座山为2Xi,每个坝为Ai.已知Ai,求出2Xi. 根据已知的X1,则可分别求出X2-n ...
- 运维脚本-elasticsearch数据迁移python3脚本
elasticsearch数据迁移python3脚本 #!/usr/bin/python3 #elsearch 数据迁移脚本 #迁移工具路径 import time,os #下面命令是用到了一个go语 ...
- css中的居中的方法
一.垂直居中 (1)inline或者inline-*元素 1. 单行文字 设置上下padding相等 以前一直以为inline元素是没有上下的padding和margin的,其实不然,他们是有上下的p ...