题目

我是按照边进行二分的

class Solution {
public:
int sum[100005]; int a[305][305];
int maxSideLength(vector<vector<int>>& mat, int threshold) { if(threshold==0)
return 0;
int n = mat.size();
int m = mat[0].size(); int len = min(n,m); for(int i=0;i<=len;i++)
{
sum[i]=99999999;
} a[0][0] = mat[0][0];
for(int j=1;j<m;j++)
{
a[0][j] = mat[0][j]+a[0][j-1]; } for(int i=1;i<n;i++)
{
a[i][0]=mat[i][0]+a[i-1][0];
} for(int i=1;i<n;i++)
{
for(int j=1;j<m;j++)
{
a[i][j] = a[i-1][j]+a[i][j-1]+mat[i][j] -a[i-1][j-1];
}
} for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
for(int k=0;k<len;k++)
{
int x=99999999;
if(i-k<0||j-k<0)
continue;
if(k==0)
{
x = mat[i][j];
}
else
{
x=a[i][j];
if(i-k-1>=0)
{
x = x-a[i-k-1][j];
}
if(j-k-1>=0)
{
x = x-a[i][j-k-1];
}
if(i-k-1>=0&&j-k-1>=0)
{ x = x+a[i-k-1][j-k-1];
} } if(x<=threshold)
{
sum[k+1] = min(sum[k+1],x);
}
}
}
} int start = 1;
int end = len; int ans=-1; while(start<=end)
{
int mid = (start + end)/2; if(sum[mid]>threshold)
{
end = mid-1;
} if(sum[mid]<threshold)
{
start = mid+1;
} if(sum[mid]==threshold)
{
ans=mid;
break;
}
} if(ans==-1)
{
if(sum[end]>threshold)
ans=0;
else
ans=end;
}
return ans; }
};

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_[二维前缀和]

    题目链接 Given a m x n matrix mat and an integer threshold. Return the maximum side-length of a square w ...

  3. [LeetCode] 718. Maximum Length of Repeated Subarray 最长的重复子数组

    Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...

  4. LeetCode 1239. Maximum Length of a Concatenated String with Unique Characters

    原题链接在这里:https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters ...

  5. LeetCode 53. Maximum Subarray(最大的子数组)

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  6. LeetCode 325. Maximum Size Subarray Sum Equals k

    原题链接在这里:https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/ 题目: Given an array nums an ...

  7. [LeetCode] 325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  8. [LeetCode] 628. Maximum Product of Three Numbers 三个数字的最大乘积

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  9. [Bug]The maximum array length quota (16384) has been exceeded while reading XML data.

    写在前面 在项目中,有客户反应无法正常加载组织结构树,弄了一个测试的程序,在日志中查看到如下信息: Error in deserializing body of reply message for o ...

随机推荐

  1. Nginx反向代理Tomcat静态资源无法加载以及请求链接错误

     在使用Nginx实现Tomcat的负载均衡的时候,项目发布到了Tomcat,Nginx也配置好了, 当访问的时候发现了与预期不符 表现为: 静态资源加载失败 链接跳转地址错误 下面是我错误的配置文件 ...

  2. Ubuntu 安装最新版nodejs

    转自:ubuntu快速安装最新版nodejs,只需2步 第一步,去 nodejs 官网 https://nodejs.org 看最新的版本号: 也就是说此时此刻,12.6.0 是最新的版本,不过你求稳 ...

  3. python基础(21):异常处理

    1. 异常和错误 1.1 错误 程序中难免出现错误,而错误分成两种 1.1.1 语法错误 语法错误:这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正. #语法错误示范一 if ...

  4. struts图片上传

    文件上传:三种上传方案1.上传到tomcat服务器 上传图片的存放位置与tomcat服务器的耦合度太高2.上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系文件服 ...

  5. Android开发当中的JavaBean实现

    一般我们在Android开发当中如果会对一些数据类进行解析,那么则需要写出一个JavaBean的类,比如在进行json解析的时候,就需要使用这个类进行数据的处理,下面是我们的JavaBean的模板代码 ...

  6. 基于socketsever下实现的FTP

    # ### 客户端client import socket import json import struct import os sk = socket.socket() sk.connect( ( ...

  7. PyCharm注释中出现中文运行报错的解决办法

    SyntaxError: Non-UTF-8 code starting with '..... 方法一:在文件首行加上 # -*- coding:utf-8 -*- 方法二:更改编码格式 File ...

  8. Shell命令-系统信息及显示之free、cal

    文件及内容处理 - free.cal 1. free:查看系统内存 free命令的功能说明 free 命令用于显示内存状态.free 指令会显示内存的使用情况,包括实体内存,虚拟的交换文件内存,共享内 ...

  9. 3.Python网络编程_多任务问题抛出

    #单线程程序 import time def sing(): """唱歌5秒钟""" for i in range(5): print(&q ...

  10. 小程序的flex布局

    小程序建议使用flex布局进行排版 flex就是一个盒装弹性布局 flex是一个容器,所有子元素都是他的成员 小程序的flex布局 定义布局 display:flex flex容器的属性: flex- ...