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 ...
随机推荐
- wpf/winform获取windows10系统颜色和主题色
Windows10开始微软在系统颜色中添加了深色,对于UWP来说很轻松就能获取到系统当前的颜色和主题色,而对于Win32应用就没有那么直观了. 在wpf中,可以通过SystemParameters.W ...
- linux shell通过curl获取HTTP请求的状态码
直接上代码: curl -I -m -o /dev/null -s -w %{http_code} www.baidu.com 参数说明: -I 仅测试HTTP头 -m 10 最多查询10s -o / ...
- linux下 sort | uniq | wc | less 几个命令的基本用法
sort -f :忽略大小写的差异,例如 A 与 a 视为编码相同: -b :忽略最前面的空格符部分: -M :以月份的名字来排序,例如 JAN, DEC 等等的排序方法: -n :使用『纯数字』进行 ...
- 邮Z速递物流,让用户密码在网络中遨游
" 最近分析快递行业的APP上瘾了,求解救." 邮政作为快递行业一个傻大黑的存在,一直很奇怪,我一直在纳闷,邮政和EMS到底是不是一家,在很多网点,它们是一体的存在,但很多东西,又 ...
- arcgis api for javascript 学习(五) 实现地图绘制工具
1.本文实现的功能为鼠标与地图之间的交互,能够在地图上绘制不同形状的图形 2.代码部分主要讲到的为Graphic函数的相关功能 <!DOCTYPE html> <html> & ...
- dijit/_WidgetBase
英文地址:http://dojotoolkit.org/reference-guide/1.10/dijit/_WidgetBase.html 介绍 dijit/_WidgetBase是Dijit包中 ...
- docker 安装 rabbitMQ服务器
1. 镜像拉取 拉取rabbitMQ镜像文件,后边要接上 management 表名是拉取带有web管理端的镜像,有web界面方便管理. 2.默认用户运行镜像 docker run -d -p 567 ...
- arm-linux-gcc-5.4.0安装方法
首先需要下载arm-linux-gcc的安装包 这里提供一个5.4.0版本的安装包,如有需要自行下载. 下载链接:https://pan.baidu.com/s/1prpdmVNWBFzg79OXQs ...
- 12. final修饰符
一.final修饰符概述 1. final可以修饰类.变量和方法 2. final修饰的类.变量和方法不可改变 3. 不允许为final变量重新赋值,子类不允许覆盖父类的final方法,final类不 ...
- c语言程序设计第1章
开始记录自己学习c语言的过程,选用的教材是浙江大学何钦铭.颜晖编写的<c语言程序设计>(第3版). 一.第一个程序 /*求阶乘问题.输入一个正整数n,输出n!*/ #include< ...