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 ...
随机推荐
- CSV文件数据如何读取、导入、导出到新的CSV文件中以及CSV文件的创建
CSV文件数据如何读取.导入.导出到新的CSV文件中以及CSV文件的创建 一.csv文件的创建 (1)新建一个文本文档: 打开新建文本文档,进行编辑. 注意:关键字与关键字之间用英文半角逗号隔开.第一 ...
- 设计模式-单例模式(winfrom带参)
一.单例模式 就是在整个代码全局中,只有一个实例.比如Log4.NET或者窗体程序. 二.实战演练 通过字段cSOCode获取窗体,窗体只有一个且cSOCode值不同获取的窗体也不同. private ...
- Python 使用 PyMysql、DBUtils 创建连接池,提升性能
转自:https://blog.csdn.net/weixin_41287692/article/details/83413775 Python 编程中可以使用 PyMysql 进行数据库的连接及诸如 ...
- java基础(16):正则表达式、Date、DateFormat、Calendar
1. 正则表达式 1.1 正则表达式的概念 正则表达式(英语:Regular Expression,在代码中常简写为regex). 正则表达式是一个字符串,使用单个字符串来描述.用来定义匹配规则,匹配 ...
- TreeMap源码分析,看了都说好
概述 TreeMap也是Map接口的实现类,它最大的特点是迭代有序,默认是按照key值升序迭代(当然也可以设置成降序).在前面的文章中讲过LinkedHashMap也是迭代有序的,不过是按插入顺序或访 ...
- LinkedHashMap,源码解读就是这么简单
概述 LinkedHashMap是HashMap的子类,它的大部分实现与HashMap相同,两者最大的区别在于,HashMap的对哈希表进行迭代时是无序的,而LinkedHashMap对哈希表迭代是有 ...
- django1-web开发基础知识
1.http概述 当前版本:1.1 http协议是一个客户端和服务端请求应答的标准TCP,如浏览器作为客户端发送请求到服务器指定端口 ,服务器将内容返回给服务器 2.协议格式 http定义了客户端与服 ...
- IDEA创建maven web工程
一.新建一个maven web工程 step1 File --> New --> Project step2 按下图步骤操作 step3 填写项目信息 step4 选择本地的maven安装 ...
- 开源项目Telegram源码 Telegram for Android Source
背景介绍 Telegram 是一款跨平台的即时通信软件,它的客户端是自由及开放源代码软件.用户可以相互交换加密与自毁消息,发送照片.影片等所有类型文件.官方提供手机版.桌面版和网页版等多种平台客户端. ...
- 深入理解JVM虚拟机(二):JDK 内存类的异常分析
JVM数据存储 堆存储(Heap):对象存储,实际上就是JAVA的数据存储 方法堆栈(Method Stack):存储方法调用的关系. 永久代(Perm):在JDK1.6及之前,常量数据存储于此区域 ...