题目

我是按照边进行二分的

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. hello tensorflow,我的第一个tensorflow程序

    上代码: import tensorflow as tf if __name__=='__main__': g = tf.Graph() # add ops to the user created g ...

  2. python凯撒加密

    在密码学中,恺撒密码是一种最简单且最广为人知的加密技术.它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文.例,当偏移量是3的时候,所有的字母A将 ...

  3. Jquery选择器个人总结

    1.选择第一级子节点 通过> 或者children方法实现 $('#XtraTabPage8>.datagrid-ftable') $('#XtraTabPage8').children( ...

  4. Junit4模板

    模板 MallApplicationTests import org.junit.runner.RunWith; import org.springframework.boot.test.contex ...

  5. Tomcat启动分析(二)-自己编译Tomcat

    为了方便分析Tomcat源码,利用大家习惯的方式来进行Debug调试,那么如何将Tomcat源码导入到Eclipse呢,这就是本文的重点 1 准备 1.1 获取Tomcat源码 获取tomcat源码有 ...

  6. JavaScript—字符串(String)用法

    字符串(String)去除空格 str = " hello python " // 去除左空格: str=str.replace( /^\s*/, ''); // 去除右空格: s ...

  7. react相关小技巧

    一.我们在项目中切换路由的时候可能会遇到 Warning: setState(...): Can only update a mounted or mounting component. This u ...

  8. JS之for循环面试题

    今天同事问了道问题 ,b=; ,b<;++a,++b){ g=a+b } console.log(g) 问输出结果为多少??? 答案:12 这里要知道for循环的条件不管写多少个,必须都满足才可 ...

  9. Redis学习笔记(六、哨兵)

    目录: 基本概念 环境部署 哨兵原理 哨兵命令 基本概念: 1.什么是哨兵 我们先从字面意思来了解哨兵,哨兵是对执行警戒任务的士兵的统称:在redis中哨兵也是一样,他监控着redis服务器的状态. ...

  10. jQuery function函数详解

    一.$(function(){}); $(document).ready(function(){})可以简写成$(function(){}); $(document).ready 里的代码是在页面内容 ...