题目

我是按照边进行二分的

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. 死磕 java同步系列之ReentrantReadWriteLock源码解析

    问题 (1)读写锁是什么? (2)读写锁具有哪些特性? (3)ReentrantReadWriteLock是怎么实现读写锁的? (4)如何使用ReentrantReadWriteLock实现高效安全的 ...

  2. Ubuntu桌面版安装小记

    之前一直是用Ubuntu的桌面版装docker,桌面版本身用起来比较方便,但开销较大,如果建立多个虚拟机就比较费劲了.今天试了一下server版的Ubuntu,发现还是比较方便的,相比桌面版,有如下好 ...

  3. asp.net单一登录

    asp.net 使用 Application 限制单一登录 原理:用户登录后系统会分配一个与用户唯一对应的SessionID,将当前用户ID与其SessionID对应保存在Application中,一 ...

  4. HTML常用标签一

    html文本格式化标签 在网页中,有时需要为文字设置粗体 .斜体 或下划线 效果,这是就需要用到HTML中的文本格式标签,是文字以特殊的方式显示 标签语义:突出重要性,比普通文字更重要 语义 标签 说 ...

  5. kafka集群在消息消费出现无法找到topic分区的处理解决

    最近几天在做spark数据同步过程中,中间通过kafka集群处理消息,每次同步到一半就会出现同步不了 查看日志如下: 最开始看到这个问题很懵逼,完全找不到解决问题的切入口,期间也询问了架构师-因为这个 ...

  6. js动态设置padding-top遇到的坑

    我想通过js动态设置元素padding-top的百分比值:以下几种都是无法设置成功的: // setAttribute设置padding-top并且转换为百分比 imageBox.setAttribu ...

  7. Android8.1源码编译实践(Mac)

    第0步:版本选择 AOSP版本选择很重要,如果选错了,会造成编译失败等各种问题,编译AOSP对Xcode的版本是有要求的: 比如:AOSP6.0-7.0,要求Xcode的版本是8.3,然而在MacOS ...

  8. linux下配置jdk,tomcat,mysql,redis,zookeeper

    jdk: 解压: tar zxvf jdk-8u144-linux-x64.tar.gz 执行:vi /etc/profile export JAVA_HOME=/usr/local/jdk1.8.0 ...

  9. [PHP] PHP调用IMAP协议读取邮件类库

    socket.php 为连接socket的类库 imap.php 基于socket的imap协议封装 test.php 进行测试 require_once 'socket.php'; require_ ...

  10. 谈谈你对OOM的理解?

    (1)整体架构 (1)ByteBuffer使用native方法,直接在堆外分配内存. 当堆外内存(也即本地物理内存)不够时,就会抛出这个异常     ----GC Direct buffer memo ...