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<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的更多相关文章
- 【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 ...
- 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 ...
- [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 ...
- LeetCode 1239. Maximum Length of a Concatenated String with Unique Characters
原题链接在这里:https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters ...
- LeetCode 53. Maximum Subarray(最大的子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode 325. Maximum Size Subarray Sum Equals k
原题链接在这里:https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/ 题目: Given an array nums an ...
- [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 ...
- [LeetCode] 628. Maximum Product of Three Numbers 三个数字的最大乘积
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- [Bug]The maximum array length quota (16384) has been exceeded while reading XML data.
写在前面 在项目中,有客户反应无法正常加载组织结构树,弄了一个测试的程序,在日志中查看到如下信息: Error in deserializing body of reply message for o ...
随机推荐
- JQuery学习笔记(1)——选择器
JQuery本质上还是JavaScript,是JavaScript的一个框架,可以让我们更简洁地去使用JavaScript 使用之前,记得在html头部引用JQuery 通过选择器获得JQuery对象 ...
- Windows Server 2012 R2更新(KB2919355)
必须按以下顺序安装这些KB:clearcompressionflag.exe,KB2919355,KB2932046,KB2959977,KB2937592,KB2938439和KB2934018. ...
- go开发注意事项和dos的一些操作
不需要加分号 写法 go编译器一行一行编译,所以多条语句不能写在同一行,否则会报错 go语言定义的变量或者import的包如果没有使用到,代码不能通过编译 func main() { ... } 只能 ...
- Python爬虫使用selenium爬取qq群的成员信息(全自动实现自动登陆)
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: python小爬虫 PS:如有需要Python学习资料的小伙伴可以 ...
- PHP面试题2019年小米工程师面试题及答案解析
一.单选题(共29题,每题5分) 1.PHP面向对象方法重写描述错误的是? A.子类必须继承父类 B.子类可以重写父类已有方法 C.重写之后子类会调用父类方法 D.子类也可以具有与父类同名的属性,进行 ...
- django5-书籍与出版社关联外键
1.外键相关 一对多的概念 ,这里是一个出版社对应本书籍 ! 设计表使用model models.ForeignKey('关联一', on_delete=models.CASCADE) #给多设置外键 ...
- JDK内置工具命令
javap Java反编译工具,主要用于根据Java字节码文件反汇编为Java源代码文件用法:javap 用法 描述 javap -help —help -? 输出此用法消息 javap -versi ...
- paypal开发指南
一.开发者地址: https://developer.paypal.com 使用在paypal上注册的账号登陆即可, 二.沙箱账号 paypay自动会为你创建两个沙箱账号,一个商家,一个买家.在acc ...
- PLSQL 12 安装、连接Oracle
点击下载PLSQL,本次安装的PLSQL版本为12.0.7,建议安装64位. 下载PLSQL时,版本旁边会有个“Language pack”的链接,点击后左侧选择“Chinese”即可下载汉化包. 注 ...
- 【微信小程序】App.js生命周期
1.小程序的生命周期-App.js App() 必须在 app.js 中注册,且不能注册多个.所以App()方法在一个小程序中有且仅有一个. App({ onLaunch: function () { ...