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[0].size();
        vector<vector<int>> sum(row+1, vector<int>(col+1, 0));
        for(int i=1; i<=row; i++)
            for(int j=1; j<=col; j++)
                sum[i][j] = sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+mat[i-1][j-1];
        int ret = 0;
        for(int i=1; i<=row; i++)
            for(int j=1; j<=col; j++){
                for(int k=1; 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;
    }
};

原文地址:https://www.cnblogs.com/jasonlixuetao/p/12046466.html

时间: 2024-08-29 18:08:35

leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和]的相关文章

Baozi Leetcode solution 1292.&#160;Maximum Side Length of a Square with Sum Less than or Equal to Threshold

Problem Statement 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

asp.net 文件上传出错:Maximum request length exceeded 解决方法

<configuration>    <system.web>               <httpRuntime maxRequestLength="102400" useFullyQualifiedRedirectUrl="true" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="10

QRCode.js网址太长生成二维码报错:Code Length OverFlow Error

当网址的长度有2百多时,使用QRCode生成二维码报错: Code Length OverFlow Error 解决方法: 到 https://github.com/KeeeX/qrcodejs 下载 qrcode.js 或 qrcode.min.js 替换掉原来的,再刷新页面,问题搞定! 原文地址:http://blog.51cto.com/wenguonideshou/2152434

System.Web.HttpException: Maximum request length exceeded

.net程序在上传一个8M多的文件的时候报错 错误原因: 由于web.config中设置的asp.net最大支持的请求小于您所上传的文件大小,默认支持4M的. 修改方法: web.Config中<httpRuntime   requestLengthDiskThreshold   = "10240 "maxRequestLength   = "10240 "   /> 如果不起作用,则需要修改IIS默认配置 C:\WINDOWS\system32\ine

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

In system.web <httpRuntime maxRequestLength="1048576" executionTimeout="3600" /> And in system.webServer <security> <requestFiltering> <requestLimits maxAllowedContentLength="1073741824" /> </reques

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

QRCode.js is javascript library for making QRCode. QRCode.js supports Cross-browser with HTML5 Canvas and table tag in DOM. QRCode.js has no dependencies. Project forked from davidshimjs/qrcodejs, we fixed Code Length Overflow error. 可以下载修复的版本,亲测有效!

CF1221F Choose a Square(二维偏序)

由于y=x,我们可以将点对称过来,以便(x,y)(x<y) 考虑选取正方形(a,a,b,b),点集则为\((a\le x\le y\le b)\),相当于二维数点 将点按x降序,y升序排列,线段树先存"-坐标",以便统计\((a,b)\)时直接线段树查询最大值再加b即可 原文地址:https://www.cnblogs.com/y2823774827y/p/11629629.html

Codeforces Round #599 (Div. 2) A. Maximum Square

Ujan decided to make a new wooden roof for the house. He has nn rectangular planks numbered from 11 to nn. The ii-th plank has size ai×1ai×1 (that is, the width is 11 and the height is aiai). Now, Ujan wants to make a square roof. He will first choos

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j