题目链接

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. dependencies 和 starter

    以 spring-cloud-alibaba-dependencies-1.5.0.RELEASE 为例: <dependency> <groupId>com.alibaba. ...

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

    目录 模式简介 示例代码 代码功能与实现思路 类图 代码 结果图示分析 模式角色和类图 角色 类图 拓展思路 对多个地方产生影响 什么要共享,什么不要共享 垃圾回收 模式简介 Flyweight是轻量 ...

  3. cmd 编码修改。 牛阿。 解决问题

    http://jingyan.baidu.com/article/e75aca85440f01142edac636.html 命令窗口修改编码,CMD编码修改方法 听语音 | 浏览:9696 | 更新 ...

  4. oracle DBID,SID,DB_NAME,DB_DOMAIN,INSTANCE_NAME,DB_UNIQUE_NAME, SERVICE_NAMES 及监听参数的说明

    DBID,SID,DB_NAME,DB_DOMAIN,INSTANCE_NAME,DB_UNIQUE_NAME, SERVICE_NAMES 及监听参数的说明 DB 相关的: DBID, SIDPFI ...

  5. 【SD系列】SAP 查看销售订单时,报了一个错误消息,“项目不符合计划行(程序错误)”

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[SD系列]SAP 查看销售订单时,报了一个错误 ...

  6. Ansible安装配置(CentOS 7)

    Ansible 中的概念 任务 Task——多个 Task 顺序执行,在每个 Task 执行结束可以通知 Hanlder 触发新操作. 变量 Variable——用户定义的变量. 环境 Facts—— ...

  7. SVG.JS 画弧线

    需求描述: 使用svg.js,绘制一个弧线.下图绿色弧线. 准备工作: 1.了解SVG Path中的A指令 详细文档,请戳这里 给定x半径.y半径后,经过指定的两点,可以有2个椭圆,因此两点间有2条弧 ...

  8. jQuery源码分析系列——来自Aaron

    jQuery源码分析系列——来自Aaron 转载地址:http://www.cnblogs.com/aaronjs/p/3279314.html 版本截止到2013.8.24 jQuery官方发布最新 ...

  9. Kali系统 metasploit 使用教程

    基础配置 由于kali 2.0 已经没有metasploit 这个服务了,所以service metasploit start 的方式不起作用. 在kali 2.0中启动带数据库支持的MSF方式如下: ...

  10. javascript基础总汇

    ## javaScript是什么:1.JavaScript 运行在客户端(浏览器)的编程语言2.用来给HTML网页增加动态功能3.用来给HTML网页增加动态功能.4.Netscape在最初将其脚本语言 ...